c# - 如何在 Silverlight 中按名称获取 DependencyProperty?

标签 c# silverlight dependency-properties

情况:我有一个字符串,表示 Silverlight 中文本框的 DependencyProperty 的名称。例如:“文本属性”。我需要获取对 TextBox 的实际 TextProperty 的引用,这是一个 DependencyProperty。

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

DependencyPropertyDescriptor 之类的东西在 Silverlight 中不可用。看来我必须诉诸反射(reflection)才能获得引用。有什么建议吗?

最佳答案

你需要反射(reflection):-

 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");

关于c# - 如何在 Silverlight 中按名称获取 DependencyProperty?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2220474/

相关文章:

c# - 阻塞的线程可以触发事件吗?

c# - Lambda 函数导致带有 0 个参数的编译器错误,带有 1 个或多个参数的异常

c# - 计算 Property Changed MVVM 上两个值的结果

c# - 为什么 Silverlight 5 Assembly.LoadFrom 声明为内部?

c# - 在 Google URL Shortener API 调用中获取 System.Net.WebException : The remote server returned an error: (403) Forbidden.

c# - 我如何创建 double[,] 的行列表

c# - 在 Windows Phone 7 中创建一个简单的测验应用程序

wpf - 如何从我的代码隐藏构造函数访问DependencyProperty值?

c# - C# 中的简单依赖属性和 UserControl 问题

wpf - 当新值相同时,DependencyProperty 不会触发 ValueChanged