zoukankan      html  css  js  c++  java
  • Property Value Inheritance Tip(1)

    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.

  • 相关阅读:
    深度学习之TensorFlow(一)——基本使用
    64位win10+cuda8.0+vs2013+cuDNN V5下Caffe的编译安装教程并配置matlab2014a 接口
    Win10+vs2012+cuda8.0的安装与配置
    图像处理与matlab实例之图像平滑(一)
    Windows下pycharm使用theano的方法
    Python中的支持向量机SVM的使用(有实例)
    混淆矩阵在Matlab中PRtools模式识别工具箱的应用
    模式识别与机器学习—bagging与boosting
    微服务架构下分布式事务解决方案——阿里GTS
    谈谈分布式事务
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/1959538.html
Copyright © 2011-2022 走看看