确定 WPF DependencyProperty 值是否被继承

发布于 2024-07-17 16:26:50 字数 118 浏览 5 评论 0原文

有谁知道如何确定 WPF 属性的值是否被继承? 特别是,我试图确定 FrameworkElementDataContext 是从父级继承还是直接在元素本身上设置。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

‘画卷フ 2024-07-24 16:26:50

DependencyPropertyHelper.GetValueSource 将为您提供 ValueSource,其中包含用于检索 BaseValueSource< 的属性/代码>BaseValueSource 枚举告诉您 DependencyProperty 从何处获取其值,例如从父级继承、通过样式设置或在本地设置。

DependencyPropertyHelper.GetValueSource will give you a ValueSource, which includes a property for retrieving the BaseValueSource. The BaseValueSource enumeration tells you where the DependencyProperty is getting its value from, such as inherited from parent, set via a style or set locally.

怀里藏娇 2024-07-24 16:26:50

经过更多挖掘后更新

有一个 ReadLocalValue 方法是愚蠢的 Read 而不是 Get,在智能感知中很难发现。 (我认为 Apress 的 WPF 书中实际上对此有注释。)它将返回 UnsetValue(如果尚未设置该值)。

if (ReadLocalValue(Control.DataContextProperty) != 
    DependencyProperty.UnsetValue)
{
    // Data context was set locally.
}

如果您出于某种原因需要获取所有本地设置的属性,则可以使用 LocalValueEnumerator。

LocalValueEnumerator enumerator = GetLocalValueEnumerator();
while (enumerator.MoveNext())
{
    if (enumerator.Current.Property == Control.DataContextProperty)
    {
        // DataContext was set locally
    }
}

这两种方法确实让我感到好奇。 在 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 (ReadLocalValue(Control.DataContextProperty) != 
    DependencyProperty.UnsetValue)
{
    // Data context was set locally.
}

If you for some reason need to get all locally set properties, you can use LocalValueEnumerator.

LocalValueEnumerator enumerator = GetLocalValueEnumerator();
while (enumerator.MoveNext())
{
    if (enumerator.Current.Property == Control.DataContextProperty)
    {
        // DataContext was set locally
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文