元素可以从其在树中的父级继承依赖项属性的值。
说明: 属性值继承行为并未针对所有的依赖项属性在全局启用,因为继承的计算时间确实会对性能产生一定的影响。属性值继承通常只 有在特定方案指出适合使用属性值继承时才对属性启用
作用一类 Button 的 property: I am NOT red! I am styled red
<StackPanel.Resources>
</StackPanel.Resources>
I am NOT red!
I am styled red
属性值继承是包容继承
父元素还可以通过属性值继承来获得其值,因此系统有可能一直递归到页面根元素。属性值继承不是属性系统的默认行为;属性必须 用特定的元数据设置来建立,以便使该属性能够对子元素启动属性值继承。属性值继承则是关于属性值如何
当您定义自己的属性并需要它们支持 WPF功能的诸多方面,例如:样式、数据绑定、继承、 动画和默认值等,应将其实现为依赖项属性。
设计依赖项属性:
public class MyStateControl : ButtonBase
{
public MyStateControl() : base() { }
public Boolean State
{
get { return (Boolean)this.GetValue(StateProperty); }
set { this.SetValue(StateProperty, value); }
}
public static readonly DependencyProperty StateProperty = DependencyProperty.Register(
“State”, typeof(Boolean), typeof(MyStateControl), new PropertyMetadata(false));
}
设计附加属性:
public static readonly DependencyProperty IsBubbleSourceProperty = DependencyProperty.RegisterAttached(
"IsBubbleSource",
typeof(Boolean),
typeof(AquariumObject),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender)
);
public static void SetIsBubbleSource(UIElement element, Boolean value)
{
element.SetValue(IsBubbleSourceProperty, value);
}
public static Boolean GetIsBubbleSource(UIElement element)
{
return (Boolean)elem
}