wpf – 依赖/附加属性如何在内部工作以及值存储在何处?

所有这些魔法让我有点不清楚.
据我所知,依赖属性从DependencyObject继承,因此存储了值:

>在实例中如果赋值(在本地字典中)
>如果未指定值,则从指向父元素的链接获取.

protected object GetValue(string propertyName)
{
   if (LocalValues.ContainsKey(propertyName))
   {
      return LocalValues[propertyName];
   }
   return Parent.GetValue(propertyName);
}

我这是对的吗?

我也不明白附加属性的值存储在哪里?

Control.FontSizeProperty = TextElement.FontSizeProperty.AddOwner(
typeof(Control),new FrameworkPropertyMetadata(SystemFonts.MessageFontSize,FrameworkPropertyMetadataOptions.Inherits));

Addached属性上的AddOwner方法调用是否为实例字段赋值?什么时候发生这种情况,价值在哪里?

谢谢!

WPF中的属性系统非常复杂. MSDN确实有很多信息,但通常很难找到. While there are many ways a DependencyProperty can be set,我不确定你需要关心值的存储位置.

对于本地值,您可以假设它存储在DependencyObject上(同样您不应该关心它存储在何处),但需要注意的是它们不是基于字符串存储的.它确实与DependencyProperty的实例相关联.这就是您希望将属性添加属性的原因.如果有人在你的控件上设置TextElement.FontSize,就像设置你的本地FontSize属性一样.

就从父级继承属性的值而言,这仅适用于附加属性.从MSDN entry开始,用于FrameworkPropertyMetadataOptions:

Although property value inheritance might appear to work for nonattached dependency properties,the inheritance behavior for a nonattached property through certain element boundaries in the runtime tree is undefined. Always use Registerattached to register properties where you specify Inherits in the Metadata.

相关文章

迭代器模式(Iterator)迭代器模式(Iterator)[Cursor]意图...
高性能IO模型浅析服务器端编程经常需要构造高性能的IO模型,...
策略模式(Strategy)策略模式(Strategy)[Policy]意图:定...
访问者模式(Visitor)访问者模式(Visitor)意图:表示一个...
命令模式(Command)命令模式(Command)[Action/Transactio...
生成器模式(Builder)生成器模式(Builder)意图:将一个对...