返回MVVM中的单例值以进行绑定
我试图弄清楚是否可以使用单例中的值作为我的绑定。 我想做这样的事情?
public class MySingleton : INotifyPropertyChanged { //...inotifypropertychanged and singleton implementation private bool _isChecked; public bool IsChecked { get { return _isChecked; } set { _isChecked= value; OnPropertyChanged("IsChecked"); } } //...other implementation } public class MyViewModel : INotifyPropertyChanged { //...inotifypropertychanged and other implementation public bool IsAllChecked { get { return MySingleton.GetInstance().IsChecked; } } }
一些Xaml:
我试过这个,绑定似乎没有更新。 我用ObservableCollection
尝试了这个并且它工作得很好但其他类型却没有。 我认为它是ObservableCollection
特有的东西。
MyViewModel
不会为IsAllChecked
属性引发PropertyChanged
,UI中的更新也不会发生( ObservableCollection
是完全不同的情况 – INotifyCollectionChanged
)。
为什么不直接声明Instance
属性而不是GetInstance()
方法并绑定到MySingleton.Instance
?
"{Binding Path=IsChecked, Source={x:Static myNameSpace:MySingleton.Instance}}"