属性更改通知(INotifyPropertyChanged)——针对ObservableCollection

it2022-06-22  77

问题

在开发webform中,wpf中的ObservableCollection<T>,MSDN中说,在添加项,移除项时此集合通知控件,我们知道对一个集合的操作是CURD但是恰恰没有Update的时候提供集合通知,也就是说当我Update的时候,虽然"集合内容“已被修改,但是"控件“却没有实现同步更新INotifyPropertyChanged提供了解决方案。

方案1:INotifyPropertyChanged

传统方式,实现接口INotifyPropertyChanged

public class StudentByINotifyPropertyChanged: INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; //实现INotifyPropertyChanged接口 private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } private string sex; private string name; public string Sex { get { return sex; } set { sex = value; NotifyPropertyChanged("Sex"); } } public string Name { get { return name; } set { name = value; NotifyPropertyChanged("Name"); } } }

示例代码

https://github.com/zLulus/NotePractice/tree/dev3/WPF/WpfDemo/PropertyChanged

方案2:采用框架实现好的

mvvmlight的ViewModelBase已实现该方法,使用如下

 

 

List与ObservableCollection对比

List可检查更改,不能检查增加、删除ObservableCollection检查增加、删除,不能检查更改

转载于:https://www.cnblogs.com/Lulus/p/8158367.html

相关资源:数据结构—成绩单生成器

最新回复(0)