2016-07-26 76 views
1

我有一個依賴屬性的用戶控件是一個枚舉。我綁定到它並將其設置在主窗口視圖中,但它不會更改。枚舉依賴屬性永遠不會從視圖更新

這是用戶控件和圖標是枚舉

<local:GoogleMaterialIcon Icon="AccountBalance"/> 

這裏是枚舉

public enum Icon 
{ 
    _3DRotation, 
    Accessibility 
}; 

這裏是DP

/// <summary> 
    /// Dependency Property used to back the <see cref="Icon"/> Property 
    /// </summary> 
    public static readonly DependencyProperty IconProperty = 
     DependencyProperty.Register("Icon", 
      typeof(Icon), 
      typeof(GoogleMaterialIcon), 
      new PropertyMetadata(null)); 

最後的財產

public Icon Icon 
{ 
    get { return (Icon)GetValue(IconProperty); } 
    set 
    { 
     SetValue(IconProperty, value); 
    } 
} 

我在設置圖標中放置了一個斷點,但它永遠不會運行。枚舉也在它自己的文件中。我每次運行它表明我錯了圖標,因爲DP恢復到第一個枚舉和永遠不會更新

更新:背後

public partial class GoogleMaterialIcon : UserControl 
{ 
    /// <summary> 
    /// Dependency Property used to back the <see cref="Icon"/> Property 
    /// </summary> 
    public static readonly DependencyProperty IconProperty = 
     DependencyProperty.Register("Icon", 
      typeof(Icon), 
      typeof(GoogleMaterialIcon), 
      new PropertyMetadata(null)); 


    /// <summary> 
    /// Constructor 
    /// </summary> 
    public GoogleMaterialIcon() 
    { 
     InitializeComponent(); 
    } 

    /// <summary> 
    /// Select a predefined icon to use 
    /// </summary> 
    public Icon Icon 
    { 
     get { return (Icon)GetValue(IconProperty); } 
     set 
     { 
      SetValue(IconProperty, value); 
     } 
    } 
} 
+0

添加了usercontrol的後面代碼。我不使用視圖中的屬性呢。這個想法是在你缺少PropertyChanged-Event的'PropertyMetadata'中的圖標 – Sam

+0

的設置器中執行某些操作。哦,你實際上應該**將你的財產綁定到正確的類型。不僅設置字符串 – lokusking

+0

我不必正常添加。這是一個特殊的枚舉? – Sam

回答

1

Obviosly下,一些用戶控件的完整代碼 - 至少對我來說 - 怪情況下,即使Microsoft只有解決方案而不是解釋。

在你PropertyMetadata你缺少的PropertyChanged - 活動

不幸發生了什麼在這裏,我不能在深,解釋。但使用PropertyChanged-DependencyProperty的事件似乎對我來說是一個可接受的解決方法。

+0

該屬性改變了事件的作品,你可以把它作爲一個答案,我會標記它的問題謝謝,只是爲了清楚起見,我嘗試了正面的價值,它不幸的工作 – Sam