2012-05-24 55 views
1

我有一個包含我的自定義的DependencyProperty這樣的控制:獲取自定義依賴屬性

public static readonly DependencyProperty MouseEnterColorProperty = 
      DependencyProperty.Register("MouseEnterColor", typeof (Color), typeof (SCADAPolyline), new PropertyMetadata(default(Color))); 

     public Color MouseEnterColor 
     { 
      get { return (Color) GetValue(MouseEnterColorProperty); } 
      set { SetValue(MouseEnterColorProperty, value); } 
     } 

其怪異的疑難問題。我使用反射來獲取我的屬性設置新value.But不能讓我property.I試圖從type.GetFields每一種可能性()

FieldInfo fieldInfo = type.GetField(name, BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static); 

or 

fieldInfo = type.GetFields(BindingFlags.Static | BindingFlags.Public) 
              .Where(p => p.FieldType.Equals(typeof(DependencyProperty)) && p.Name==name).FirstOrDefault(); 

聽起來像我的財產是missing.I着訪問這個問題讓我很生氣。 我如何解決這個問題的任何想法?我正在使用silverlight 5.0

+0

你調試的是'name'?爲什麼不嘗試獲取非靜態的'MouseEnterColor'包裝? – nemesv

+0

是的,我檢查了它的名字是否正確。 –

+0

+1,用於描述和編碼。總是幫助:) –

回答

0

依賴項屬性不是字段。它不是通常意義上的類定義的一部分。

在幕後,它存儲在依賴屬性的集合中。

試試這個例子from here關於如何訪問它們指南:

public static class DependencyObjectHelper 
    { 
     public static List<DependencyProperty> GetDependencyProperties(Object element) 
     { 
      List<DependencyProperty> properties = new List<DependencyProperty>(); 
      MarkupObject markupObject = MarkupWriter.GetMarkupObjectFor(element); 
      if (markupObject != null) 
      { 
       foreach (MarkupProperty mp in markupObject.Properties) 
       { 
        if (mp.DependencyProperty != null) 
        { 
         properties.Add(mp.DependencyProperty); 
        } 
       } 
      } 

      return properties; 
     } 
+0

Thx.This是我想找的:) –