我試圖將一些類的靜態屬性綁定到某些控件。我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接口中定義的不是靜態的,但在這裏它是靜態的)。
此時我放棄了。
任何想法?
豎起大拇指我實施了一個不同的見下文 – 2015-07-22 00:42:19