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

c# – 如何通过Silverlight中的名称获取DependencyProperty?

发布时间:2020-12-15 17:41:13 所属栏目:百科 来源:网络整理
导读:情况:我有一个字符串,表示Silverlight中TextBox的DependencyProperty的名称.例如:“TextProperty”.我需要引用TextBox的实际TextProperty,它是一个DependencyProperty. 问题:如果我获得的是属性的名称,我如何获得对DependencyProperty的引用(在C#中) 像De
情况:我有一个字符串,表示Silverlight中TextBox的DependencyProperty的名称.例如:“TextProperty”.我需要引用TextBox的实际TextProperty,它是一个DependencyProperty.

问题:如果我获得的是属性的名称,我如何获得对DependencyProperty的引用(在C#中)

像DependencyPropertyDescriptor这样的东西在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");

(编辑:李大同)

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

    推荐文章
      热点阅读