返回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
?
上述就是C#学习教程:返回MVVM中的单例值以进行绑定分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
"{Binding Path=IsChecked, Source={x:Static myNameSpace:MySingleton.Instance}}"
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1008744.html