如何最轻松地确定某个属性是否为依赖属性?

发布于 2024-11-26 19:03:47 字数 443 浏览 1 评论 0 原文

我最近遇到了将数据绑定到 DataGridTextColumn 的 Visibility 属性的问题。造成混乱的原因是此属性在 WPF 中是依赖属性,但在 Silverlight 中不是。

我认为 MSDN 文档并没有说得很清楚。以下是 WPF 的唯一相关文本。

“有关影响该值的因素的信息,请参阅 DependencyProperty。”

http:// msdn.microsoft.com/en-us/library/system.windows.controls.datagridcolumn.visibility(v=VS.100).aspx

I recently had an issue with databinding to the Visibility property of a DataGridTextColumn. The confusion arose because this property is a dependecy property in WPF but not in Silverlight.

I don't think that the MSDN documentation makes this very clear. The following is the only related text for WPF.

"For information about what can influence the value, see DependencyProperty."

http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridcolumn.visibility(v=VS.100).aspx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

妞丶爷亲个 2024-12-03 19:03:47

依赖属性在定义它们的类上有一个相应的静态字段。请查看 DataGridTextColumn 类。

Dependency properties have a corresponding static field on the class they are defined in. Have a look at the fields section of the DataGridTextColumn class.

世态炎凉 2024-12-03 19:03:47

在大多数情况下,您可以通过检查是否存在类型为 DependencyProperty 的名为 FooProperty 的静态字段来检测属性 Foo 是否是 DP。然而,这只是一个约定。不能保证所有依赖属性都遵循此模式。

In most cases you can detect whether a property Foo is a DP by checking if there is a static field named FooProperty of type DependencyProperty. However, this is only a convention. There is no guarantee that all dependency properties will follow this pattern.

再见回来 2024-12-03 19:03:47

已经回答了,我知道。 IE。 “TextBlock”中的“Text”属性是您可以辨别的依赖属性,因为 Intellisense 显示如下静态字段:

TextBlock.TextProperty

Already answered, I know. IE. The "Text" property in a "TextBlock" is a dependency property you can tell because Intellisense shows the static filed like this:

TextBlock.TextProperty

软甜啾 2024-12-03 19:03:47

下面是一个扩展方法,它评估属性的值并返回相应的 DependencyProperty:

    public static bool TryEvaluateProperty(this FrameworkElement fe, string property, out object value, out DependencyProperty dp)
    {
        value = default;
        dp = default;

        var feType = fe.GetType();
        var propertyInfo = feType.GetProperty(property, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty);

        if (propertyInfo == null) return false;

        value = propertyInfo.GetValue(fe);

        var dpName = property + "Property"; //ASSUMPTION: Definition uses DependencyProperty naming convention

        while (feType != null)
        {
            var dpField = feType.GetField(dpName, BindingFlags.Static | BindingFlags.Public);

            if (dpField != null)
            {
                dp = dpField.GetValue(null) as DependencyProperty; //use obj==null for static field
                break;
            }

            feType = feType.BaseType;
        }

        return true;
    }

Here's an extension method that evaluates a property's value and also returns the corresponding DependencyProperty:

    public static bool TryEvaluateProperty(this FrameworkElement fe, string property, out object value, out DependencyProperty dp)
    {
        value = default;
        dp = default;

        var feType = fe.GetType();
        var propertyInfo = feType.GetProperty(property, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty);

        if (propertyInfo == null) return false;

        value = propertyInfo.GetValue(fe);

        var dpName = property + "Property"; //ASSUMPTION: Definition uses DependencyProperty naming convention

        while (feType != null)
        {
            var dpField = feType.GetField(dpName, BindingFlags.Static | BindingFlags.Public);

            if (dpField != null)
            {
                dp = dpField.GetValue(null) as DependencyProperty; //use obj==null for static field
                break;
            }

            feType = feType.BaseType;
        }

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