确定 WPF DependencyProperty 值是否被继承
有谁知道如何确定 WPF 属性的值是否被继承? 特别是,我试图确定 FrameworkElement
的 DataContext
是从父级继承还是直接在元素本身上设置。
Does anyone know how to determine if the value of a WPF property is inherited? In particular, I'm trying to determine if the DataContext
of a FrameworkElement
was inherited from the parent or set directly on the element itself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DependencyPropertyHelper.GetValueSource
将为您提供ValueSource
,其中包含用于检索BaseValueSource< 的属性/代码>
。
BaseValueSource
枚举告诉您DependencyProperty
从何处获取其值,例如从父级继承、通过样式设置或在本地设置。DependencyPropertyHelper.GetValueSource
will give you aValueSource
, which includes a property for retrieving theBaseValueSource
. TheBaseValueSource
enumeration tells you where theDependencyProperty
is getting its value from, such as inherited from parent, set via a style or set locally.经过更多挖掘后更新
有一个
ReadLocalValue
方法是愚蠢的 Read 而不是 Get,在智能感知中很难发现。 (我认为 Apress 的 WPF 书中实际上对此有注释。)它将返回 UnsetValue(如果尚未设置该值)。如果您出于某种原因需要获取所有本地设置的属性,则可以使用 LocalValueEnumerator。
这两种方法确实让我感到好奇。 在 ReadLocalValue 中读取而不是 Get 以及无法在 GetLocalValueEnumerator 中使用 foreach 迭代的集合。 就像 .Net 有这些很好的标准东西,但 WPF 团队决定忽略它们。
Updated after more digging
There is a
ReadLocalValue
method which is stupidly Read instead of Get so hard to spot in intellisense. (I think Apress' WPF book had a note about this actually.) It will return UnsetValue if the value hasn't been set.If you for some reason need to get all locally set properties, you can use LocalValueEnumerator.
And these two methods really make me wonder. Read instead of Get in the ReadLocalValue and a collection which you cannot iterate with foreach in GetLocalValueEnumerator. It's like .Net has these nice standard things which the WPF team just decided to ignore.