【问题标题】:How to get the type that a DependencyProperty uses for its value in Silverlight?如何获取 DependencyProperty 在 Silverlight 中用于其值的类型?
【发布时间】:2016-05-17 21:46:19
【问题描述】:

我想检查 DependencyProperty 的类型是什么,在 WPF 中我可以执行以下操作:

DependencyProperty property = ...;
var typeAsString = property.PropertyType.Name;

因为 PropertyType 在 WPF 中是 only available

我想知道在 Silverlight 中是否有其他方法可以实现这一点。

【问题讨论】:

    标签: c# silverlight dependency-properties


    【解决方案1】:

    我认为这可能值得你看看。 How to get a DependencyProperty by name in Silverlight?

    你必须使用反射:

         public static DependencyProperty GetDependencyProperty(Type type, string name)
     {
         FieldInfo fieldInfo = type.GetField(name, BindingFlags.Public | BindingFlags.Static);
         return (fieldInfo != null) ? (DependencyProperty)fieldInfo.GetValue(null) : null;
     }
    

    从答案中可以看出,用法是:

     var dp = GetDependencyProperty(typeof(TextBox), "TextProperty");
    

    HTH

    【讨论】:

    • 感谢您的回复,不幸的是,我已经看到了这一点,它按预期工作,但不适用于我想要实现的目标。它会给我一个字符串中的 DP,我想要的是找到 DP 代表的类型,以便我可以创建一个“解析器”来为它设置一个来自持久性实现的值。
    猜你喜欢
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-10
    • 2014-07-26
    • 1970-01-01
    • 2021-10-11
    相关资源
    最近更新 更多