2
votes

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.

My CustomGrid has a inheritable attached property "ShowToolTip" with the Inherits flag and my CustomColumn has a "ShowToolTip" dependency property.

The CustomColumn "ShowToolTip" never takes the value set to the ShowToolTip in my CustomGrid.

CustomGrid Attached Property:

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:

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)

<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
  • Column2 (Value2): should NOT show tooltip
  • Column3 (Value3): should NOT show tooltip

What I get:

  • Column1 (Value1): shows tooltip
  • Column2 (Value2): does not show tooltip
  • Column3 (Value3): SHOWS tooltip

It seems it is taking the default value set for the property in the CustomColumn class.

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.

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?

1

1 Answers

3
votes

DataGridColumn is not placed in the VisualTree under DataGrid . It is a common misconception.

as shown here :

enter image description 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.

for Example : DataGrid's CanUserResizeColumns property which sets DataGridColumn's CanUserResize property . 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 .