开发者

How to access and provide default values to controls within WPF templates?

I'm struggling with control templates. I'm currently building a UI which has several panes which are essentially build out of more basic controls.

here's how one of our construction panes looks like right now:

<Grid>

    <StackPanel>

        <ContentControl  Template="{StaticResource ConstructionBorderCtrl}">
            <ContentControl Template="{StaticResource StringCtrl}" Content="Cash Event Value:"/>
        </ContentControl>

        <ContentControl  Template="{StaticResource ConstructionBorderCtrl}">
            <ContentControl  Template="{StaticResource RateCtrl}"></ContentControl>
        </ContentControl>

        <ContentControl Template="{StaticResource ConstructionBorderCtrl}">
            <ContentControl Grid.Row="0" Template="{StaticResource FromCtrl}"></ContentControl>
        </ContentControl>

        <ContentControl Template="{StaticResource ConstructionBorderCtrl}">
            <ContentControl Grid.Row="0" Template="{StaticResource StartEndDateCtrl}"></ContentControl>
        </ContentControl>

        <ContentControl Template="{StaticResource ConstructionBorderCtrl}">
            <ContentControl Grid.Row="0" Template="{StaticResource ComboStringCtrl}">Applicable Size:</ContentControl>
        </ContentControl>

    </StackPanel>

</Grid>

Here's a template for the StringCtrl as an example:

<ControlTemplate x:Key="StringCtrl" TargetType="ContentControl">

    <Grid Margin="5">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <TextBlock Name="ctrlText" Mar开发者_如何转开发gin="0,0,5,0" Text="{TemplateBinding Content}"></TextBlock>
        <TextBox Name="ctrlDefaultValue" Grid.Column="1" />
    </Grid>
</ControlTemplate>

As you can see from the template it's really just a label and a textbox. Now let's say I wanted to provide a default value to the text box as well as perform validation on user input, but I want to provide that context from the parent Construction pane and bind it to the individual elements inside the templates. How would I go about doing that?


This is certainly a design I've never seen before. I'd think UserControls or some other type of custom control would work better for this than the ControlTemplate approach.

But if you definitely want to go down this route, I could maybe see a Behavior working for you if there's some consistency to your structure/naming in the templates - you can set properties on the behavior and the behavior can access the control via its AssociatedObject property to be able to set the values of the children and do validation.

Seems like a lot of work to me though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜