正确的模板绑定到DependencyProperty

时间:2013-01-07 21:19:07

标签: c# wpf dependency-properties templatebinding

我知道当我写这篇文章时这是不对的,我已经能够收集another answer的大部分答案,但却无法掌握最后一点。 绑定确实从UI传递到DependencyProperty(以及创建控件时的另一种方式)。

我的模板(需要将IsChecked绑定移动到实例):

<ControlTemplate x:Key="MyHeaderedContentTemplate" TargetType="HeaderedContentControl">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="1*" />
    </Grid.RowDefinitions>
    <ContentControl>
        <StackPanel Orientation="Horizontal">
            <CheckBox Margin="2,2,20,2"
                                    Content="All/None"
                                    IsChecked="{Binding Path=AllFeatureTypesChecked, Mode=TwoWay}" />
            <TextBlock Text="{TemplateBinding Header}" Margin="2"/>
        </StackPanel>
    </ContentControl>
    <ContentControl Grid.Row="1" Content="{TemplateBinding Content}" />
</Grid>

实例:

<HeaderedContentControl Grid.Row="0" Grid.Column="2" Grid.RowSpan="2" Grid.ColumnSpan="2" Margin="4"
                    Template="{StaticResource  ResourceKey=MyHeaderedContentTemplate}"
                    Header="Feature Details by Type">
    <HeaderedContentControl.Resources>

    </HeaderedContentControl.Resources>
    <ListBox ItemTemplate="{StaticResource SelectableLinkNode}"
            ItemsSource="{Binding Features}"/>
</HeaderedContentControl>

The Content Binding provides a list of CheckBox items

Setter(当然,还有一个布尔AllFeatureTypesChecked DependencyProperty):

    /// <summary>
    /// Needs to be set on Startup and on ItemIsCheckedChanged Event from the Features List
    /// </summary>
    private void SetAllSelectedState()
    {
        bool allSelected = (Features.Count > 0);
        foreach (var CheckableItem in Features) {
            if (CheckableItem.IsChecked == false) {
                allSelected = false;
                break;
            }
        }

        SetCurrentValue(AllFeatureTypesCheckProperty, allSelected);

    }

供参考,这是DP

public static readonly DependencyProperty AllFeatureTypesCheckProperty =
        DependencyProperty.Register("AllFeatureTypesCheck", typeof(Boolean), typeof(ReportSources),
        new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnAllFeatureTypesCheckedChanged));

这是非常有趣的东西,如果没有这里真棒人,我就无法做到! 谢谢!

更新:好的,现在我有了这个(头脑风暴):

    <ControlTemplate x:Key="MyHeaderedContentTemplate" TargetType="HeaderedContentControl">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="1*" />
            </Grid.RowDefinitions>
            <ContentControl>
                <StackPanel Orientation="Horizontal">
                <ContentPresenter Content="{DynamicResource ResourceKey=CheckControl}" Margin="2,2,20,2"/>
                    <TextBlock Text="{TemplateBinding Header}" Margin="2"/>
                </StackPanel>
            </ContentControl>
            <ContentControl Grid.Row="1" Content="{TemplateBinding Content}" />
        </Grid>
    </ControlTemplate>

实例化如下:

    <HeaderedContentControl Grid.Row="0" Grid.Column="2" Grid.RowSpan="2" Grid.ColumnSpan="2" Margin="4"
                            Template="{StaticResource  ResourceKey=MyHeaderedContentTemplate}"
                            Header="Feature Details by Type"
                            >
        <HeaderedContentControl.Resources>
            <CheckBox x:Key="CheckControl"
                      Content="All/None"
                      IsThreeState="True"
                      IsChecked="{Binding Path=AllFeatureTypesChecked, Mode=TwoWay}"
                      />
        </HeaderedContentControl.Resources>
        <ListBox ItemTemplate="{StaticResource SelectableLinkNode}"
                 ItemsSource="{Binding Features}"/>
    </HeaderedContentControl>

但仍然无法在创建控件后在DP上显示设置的值。

...肯定可以在这里使用一点帮助, 感谢。

1 个答案:

答案 0 :(得分:1)

愚蠢,真的 - 或者相当愚蠢......但我责备使用字符串进行注册。 当标识符的拼写发生变化时,IDE只是不能尝试更新这些内容。

    public Boolean? AllFeatureTypesChecked
    {
        get { return (Boolean?) GetValue(AllFeatureTypesCheckedProperty); }
        set { SetValue(AllFeatureTypesCheckedProperty, value); }
    }

    #region Using a DependencyProperty as the backing store for AllFeatureTypesCheck.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty AllFeatureTypesCheckedProperty =
        DependencyProperty.Register("AllFeatureTypesCheck", typeof(Boolean?), typeof(ReportSources),
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnAllFeatureTypesCheckedChanged));
    #endregion

注意对象属性上的拼写与DP上的已注册名称。