情況:我有一個字符串,表示Silverlight中TextBox的DependencyProperty的名稱。例如:「TextProperty」。我需要獲取對TextBox的實際TextProperty的引用,這是一個DependencyProperty。如何在Silverlight中通過名稱獲取DependencyProperty?
問題:如果我得到的只是屬性的名稱,如何獲得對DependencyProperty的引用(在C#中)?
類似DependencyPropertyDescriptor的東西在Silverlight中不可用。我似乎不得不求助於反思來獲得參考。有什麼建議麼?
如果你的控制繼承了它的一些DependencyPropertys,如ComboBox.SelectedItemProperty這實際上是Primitives.Selector。 SelectedItemProperty或RadioButton.IsCheckedProperty,它實際上是Primitives.ToggleButton.IsCheckedProperty,那麼你將不得不使用FieldInfo field = type.GetField(「MyProperty」,BindingFlags.FlattenHierarchy);我結束了使用FieldInfo field = type.GetField(「MyProperty」,BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy); – Scott 2010-06-04 03:24:46