代码改变世界

Property Value Inheritance Tip(1)

2011-02-21 11:20  Clingingboy  阅读(541)  评论(0编辑  收藏  举报

 

From MSDN

Property value inheritance is a feature of the Windows Presentation Foundation (WPF) property system. Property value inheritance enables child elements in a tree of elements to obtain the value of a particular property from parent elements, inheriting that value as it was set anywhere in the nearest parent element. The parent element might also have obtained its value through property value inheritance, so the system potentially recurses all the way to the page root. Property value inheritance is not the default property system behavior; a property must be established with a particular metadata setting in order to cause that property to initiate property value inheritance on child elements.

Let’  do some fun test.

First Demo.The following code define a simple Dependency Property which have the property value inheittance feature.This feature must be define with the FrameworkPropertyMetadata

public class A:StackPanel
{
    public int MyProperty
    {
        get { return (int)GetValue(MyPropertyProperty); }
        set { SetValue(MyPropertyProperty, value); }
    }

    public static readonly DependencyProperty MyPropertyProperty =
        DependencyProperty.Register("MyProperty", typeof(int), typeof(A),
        new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.Inherits));

}

public class B : FrameworkElement
{
    public int MyProperty
    {
        get { return (int)GetValue(MyPropertyProperty); }
        set { SetValue(MyPropertyProperty, value); }
    }

    public static readonly DependencyProperty MyPropertyProperty =
    A.MyPropertyProperty.AddOwner(typeof(B),
            new FrameworkPropertyMetadata(A.MyPropertyProperty.DefaultMetadata.DefaultValue,
                   FrameworkPropertyMetadataOptions.Inherits));

}

Now,do some test data,we  can change the MyProperty’s value in Class A.And the
property value inheittance  in derived classs also will be changed.

public void Test1()
{
    A a=new A();
    B b=new B();
    a.Children.Add(b);
    a.MyProperty = 2;
    Console.WriteLine(b.MyProperty);
}

The result value:

image

Property Value Inheritance in Disconnect Tree Element

In some scenario Property Value Inheritance can not be work well.When this kind of  problem happened,there are some condition we should to know.

  1. The Property defined by Dependency Property and not by Attached Property.
  2. In the Tree,not every element have the same Inheritable Dependency Property.It means that the Inheritable Dependency Property have disconnected in the tree element.

Let’s see the following demo to explain above conditions.

public void Test2()
{
    A a = new A();
    Button btn=new Button();
    B b = new B();
    btn.Content = b;
    a.Children.Add(btn);
    a.MyProperty = 2;
    Console.WriteLine(b.MyProperty);
}

Button don’t have the MyProperty,and the panel of Class B is Button not A.In this scenario,
the value of MyProperty will not be affected by Class A.

The result:

image

Now we define a custom Button inherited from Button.We can add a Inheritable Property for this custom Button.

public class CButton : Button
{
    public int MyProperty
    {
        get { return (int)GetValue(MyPropertyProperty); }
        set { SetValue(MyPropertyProperty, value); }
    }

    public static readonly DependencyProperty MyPropertyProperty =
    A.MyPropertyProperty.AddOwner(typeof(CButton),
            new FrameworkPropertyMetadata(A.MyPropertyProperty.DefaultMetadata.DefaultValue,
                   FrameworkPropertyMetadataOptions.Inherits));
}

Test Code:

public void Test3()
{
    A a = new A();
    CButton btn = new CButton();
    B b = new B();
    btn.Content = b;
    a.Children.Add(btn);
    a.MyProperty = 2;
    Console.WriteLine(b.MyProperty);
    Console.WriteLine(btn.MyProperty);
}

In this demonstration,you may find that the Inheritable Property in CButton and B all  be worked well.

The result:
image

From above discussion we can draw some conclusion.

Inheritable Property that defined by Dependecy Property is  only affected by its parent element.