2011-01-13 142 views
4

我試圖將一些類的靜態屬性綁定到某些控件。我tryied幾個實現,但每個人都有問題:綁定靜態屬性並實現INotifyPropertyChanged

所有示例使用下一個XAML:

<Label Name="label1" Content="{Binding Path=text}"/> 

1日的做法 - 不使用INotifyPropertyChanged的

public class foo1 
{ 
    public static string text { get; set; } 
} 

的問題是,當'文本'propery改變控制不通知。

第二條本辦法 - 使用INotifyPropertyChanged的

public class foo1 : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 
    public void OnPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 

    private static string _text; 
    public static string text 
    { 
     get { return _text; } 
     set 
     { 
      _text = value; 
      OnPropertyChanged("text"); 
     } 
    } 
} 

這並不編譯,因爲OnPropertyChanged()方法也不是一成不變的,這就是所謂的靜態方法中。

第二種方法嘗試2:使OnPropertyChanged()方法static =>這不會編譯,因爲OnPropertyChanged()現在是靜態的,它會嘗試使用不是靜態的「PropertyChanged」事件。

第二種方法嘗試3:使'PropertyChanged'事件static =>這不會編譯,因爲類沒有實現'INotifyPropertyChanged.PropertyChanged'事件(該事件在'INotifyPropertyChanged接口中定義的不是靜態的,但在這裏它是靜態的)。

此時我放棄了。

任何想法?

回答

10

我建議 你只是有一個實例財產返還您的靜態屬性是這樣的:

private static string _text; 
public string text 
{ 
    get { return _text; } 
    set 
    { 
     _text = value; 
     OnPropertyChanged("text"); 
    } 
} 

然而,這使得整個結合比較毫無意義,因爲更改通知中的一個實例只創建而不是每個實例。因此,只有綁定到綁定到它所在的特定實例上的屬性纔會更新。

一個更好的方法是使用一個單例,可以看到here

2

使用單例將是最簡單和最乾淨的實現。如果你想在不使用單身人士的情況下努力工作,你可以使用以下內容。

創建一個從您的靜態屬性中調用的靜態PropertyChangedEventHandler。在創建類的新實例時,註冊以接收來自靜態事件的回調。當你得到回調時,調用OnPropertyChanged(「text」)。 與此相關的BIG問題是您在註冊靜態事件時需要使用WeakReference。否則你的對象將永遠留在身邊。我在代碼中跳過了這一步。

您需要轉發到該實例事件的原因是因爲誰註冊過的NotifyPropertyChanged需要知道誰在「發件人」(與它的實例屬性foo1的即實例)

public class foo1 : System.ComponentModel.INotifyPropertyChanged 
{ 
    // static property 
    private static string _text = "static string"; 
    public static string static_text 
    { 
     get 
     { 
      return _text; 
     } 
     set 
     { 
      _text = value; 
      OnStaticPropertyChanged("static_text"); 
     } 
    } 
    private static System.ComponentModel.PropertyChangedEventHandler staticpropChanged; 
    static protected void OnStaticPropertyChanged(string pname) 
    { 
     System.ComponentModel.PropertyChangedEventArgs e = new System.ComponentModel.PropertyChangedEventArgs(pname); 
     System.ComponentModel.PropertyChangedEventHandler h = staticpropChanged; 
     if (h != null) 
      h(null, e); 

    } 
    public foo1() 
    { 
     // really should use a weakreference here.. but leaving it out 
     // for simplicity 
     staticpropChanged += foo1_staticpropChanged; 
    } 

    void foo1_staticpropChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) 
    { 
     // map the static name to the instance name 
     if(e.PropertyName == "static_text") OnPropertyChanged("text"); 
    } 
    // instance-property forwards to static 
    public string text 
    { 
     get { return foo1.static_text; } 
     set { foo1.static_text = value; } 
    } 
+0

豎起大拇指我實施了一個不同的見下文 – 2015-07-22 00:42:19

0
public static String StatusInformation 
    { 
     get { return _StatusInformation; } 
     set { _StatusInformation = value; OnStaticPropertyChanged("StatusText"); } 
    } 

    #region Handlig Static Properties Changed 
    private static System.ComponentModel.PropertyChangedEventHandler staticpropChanged; 
    static protected void OnStaticPropertyChanged(string pname) 
    { 
     System.ComponentModel.PropertyChangedEventArgs e = new System.ComponentModel.PropertyChangedEventArgs(pname); 
     System.ComponentModel.PropertyChangedEventHandler h = staticpropChanged; 
     if (h != null) 
      h(null, e); 

    } 
    private void Handler_PropertyChange(object sender, System.ComponentModel.PropertyChangedEventArgs e) 
    { 
     NotifyPropertyChanged(e.PropertyName); 
    } 
    #endregion 
    public string StatusText 
    { 
     get { return ExchangeServices.StatusInformation; } 
     set { ExchangeServices.StatusInformation = value; } 
    } 

這樣我就不必在事件中做任何處理。 這對於爲我的整個程序創建一個狀態欄非常有幫助,並且可以在我的擴展程序中的任何位置和任何用戶控件中進行更新。

謝謝你shimpossible

相關問題