【问题标题】:Proper Template Binding to DependencyProperty正确的模板绑定到 DependencyProperty
【发布时间】:2013-01-07 21:19:07
【问题描述】:

我知道在我写它的时候这是不对的,我已经能够收集到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>

还有 Setter(当然,还有一个 Boolean 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));

这真的很有趣,如果没有 SO 上的这些很棒的人,我无法做到! 谢谢!

更新:好的,现在我知道了(头脑风暴):

    <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 上设置的值显示在 UI 上。

...当然可以在这里使用一些帮助, 谢谢。

【问题讨论】:

    标签: c# wpf dependency-properties templatebinding


    【解决方案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 上的注册名称。

    【讨论】:

    • 现在我已经安装了 ReSharper。我很想知道我所听到的有关此产品的消息是否属实。
    • 嗯,R# 是我的武器库的一个很好的补充,但它没有处理这个问题:RaisePropertyChanged("WaasStatus")
    • 好吧,事实证明,如果您尝试使用重命名标识符时弹出的 VS 重构,R# 就没有机会扫描匹配的文本。上下文菜单的 R# 部分提供了“重命名”,其中包含搜索字符串文字的选项。
    猜你喜欢
    • 1970-01-01
    • 2021-07-10
    • 1970-01-01
    • 1970-01-01
    • 2020-06-16
    • 1970-01-01
    • 1970-01-01
    • 2013-06-03
    相关资源
    最近更新 更多