加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-13 20:42:40 所属栏目:百科 来源:网络整理
导读:所有这些魔法让我有点不清楚. 据我所知,依赖属性从DependencyObject继承,因此存储了值: 在实例中如果赋值(在本地字典中) 如果未指定值,则从指向父元素的链接获取. protected object GetValue(string propertyName){ if (LocalValues.ContainsKey(propertyNa
所有这些魔法让我有点不清楚.
据我所知,依赖属性从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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读