简体   繁体   English

子级不继承依赖项属性值,而采用默认值

[英]Child doesn't inherit dependency property value and takes default value

I have a dependency property that should inherit its value from an attached property, but it never does. 我有一个依赖项属性,该属性应从附加属性继承其值,但永远不会。 It seems to always take its default value, instead. 它似乎总是采用其默认值。

My Setup 我的设定

I have a custom grid that inherits from Microsoft's DataGrid and a CustomColumn that inherits from Microsoft's DataGridTextColumn. 我有一个从Microsoft的DataGrid继承的自定义网格和一个从Microsoft的DataGridTextColumn继承的CustomColumn。

My CustomGrid has a inheritable attached property "ShowToolTip" with the Inherits flag and my CustomColumn has a "ShowToolTip" dependency property. 我的CustomGrid具有带有Inherits标志的可继承的附加属性“ ShowToolTip”,而我的CustomColumn具有“ ShowToolTip”依赖项属性。

The CustomColumn "ShowToolTip" never takes the value set to the ShowToolTip in my CustomGrid. CustomColumn“ ShowToolTip”从不接受在我的CustomGrid中设置为ShowToolTip的值。

CustomGrid Attached Property: CustomGrid附加属性:

public class CustomGrid: DataGrid
{
        static CustomGrid()
        {
             ShowToolTipProperty = DependencyProperty.RegisterAttached("ShowToolTip", typeof(bool), typeof(CustomGrid), 
                new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.Inherits));

        }

        public static bool GetShowToolTip(DependencyObject target)
        {
            return (bool)target.GetValue(ShowToolTipProperty);
        }

        public static void SetShowToolTip(DependencyObject target, bool value)
        {
            target.SetValue(ShowToolTipProperty, value);
        }

        public bool ShowToolTip
        {
            get
            {
                return GetShowToolTip(this);
            }
            set
            {
                SetShowToolTip(this, value);
            }
        }
}

CustomColumn Property: CustomColumn属性:

public class CustomColumn : DataGridTextColumn
{
        static CustomColumn ()
        {
              ShowToolTipProperty = CustomGrid.ShowToolTipProperty.AddOwner(typeof(CustomColumn ), 
            new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.Inherits));
        }

        public bool ShowToolTip
        {
           get { return (bool)GetValue(ShowToolTipProperty); }
           set { SetValue(ShowToolTipProperty, value); }
        }
}

Use in Xaml (just binding relevant properties) 在Xaml中使用 (仅绑定相关属性)

<myNamespace:CustomGrid Name="myGrid"  ShowToolTip="False">
    <DataGrid.Columns>
        <myNamespace:CustomColumn Binding="{Binding Value1}" ShowToolTip="True"/>
        <myNamespace:CustomColumn Binding="{Binding Value2}" ShowToolTip="False"/>
        <myNamespace:CustomColumn Binding="{Binding Value3}" /> 
    </DataGrid.Columns>
</myNamespace:CustomGrid >

In this example I expect: 在此示例中,我期望:

  • Column1 (Value1): should show tooltip Column1(Value1):应该显示工具提示
  • Column2 (Value2): should NOT show tooltip Column2(Value2):不应显示工具提示
  • Column3 (Value3): should NOT show tooltip Column3(Value3):不应显示工具提示

What I get: 我得到的是:

  • Column1 (Value1): shows tooltip 列1(值1):显示工具提示
  • Column2 (Value2): does not show tooltip Column2(Value2):不显示工具提示
  • Column3 (Value3): SHOWS tooltip Column3(Value3): SHOWS工具提示

It seems it is taking the default value set for the property in the CustomColumn class. 似乎它采用的是CustomColumn类中属性的默认值。

I tried changing association of CustomColumn.ShowToolTip property with the CustomGrid.ShowToolTip property but it doesn't matter what I do, it never inherits the value from the CustomGrid. 我尝试更改CustomColumn.ShowToolTip属性与CustomGrid.ShowToolTip属性的关联,但是我做什么都没关系,它从不继承自CustomGrid的值。

ShowToolTipProperty = CustomGrid.ShowToolTipProperty.AddOwner(typeof(CustomColumn ), 
                new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Inherits));

What do I need to do to make my CustomColumn.ShowToolTip property inherit its value from teh CustomGrid.ShowToolTip property? 我该怎么做才能使CustomColumn.ShowToolTip属性继承自CustomGrid.ShowToolTip属性的值?

DataGridColumn is not placed in the VisualTree under DataGrid . DataGridColumn不在VisualGrid中的DataGrid下。 It is a common misconception. 这是一个普遍的误解。

as shown here : 如这里所示:

在此处输入图片说明

All the Dependency properties that you set via the DataGrid and effect the columns are not inherited and are done in the DataGrid's Code. 您通过DataGrid设置并影响列的所有Dependency属性都不会被继承,而是在DataGrid的代码中完成。

for Example : DataGrid's CanUserResizeColumns property which sets DataGridColumn's CanUserResize property . 例如:DataGrid的CanUserResizeColumns属性,该属性设置DataGridColumn的CanUserResize属性。 have you look at the source code for an example of how it's done . 您是否看过源代码以获取有关其完成方式的示例。

you can also see that the DataGrid's Columns are stored in a Property and not as a content of the DataGrid . 您还可以看到DataGrid的Columns存储在Property中,而不是作为DataGrid的内容存储。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM