Property Value Inheritance Tip(1)
2011-02-21 11:20 Clingingboy 阅读(548) 评论(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:
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.
- The Property defined by Dependency Property and not by Attached Property.
- 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:
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.
From above discussion we can draw some conclusion.
Inheritable Property that defined by Dependecy Property is only affected by its parent element.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现