-
Notifications
You must be signed in to change notification settings - Fork 22
属性更改通知(INotifyPropertyChanged)——针对ObservableCollection
L edited this page Mar 15, 2020
·
4 revisions
在开发webform中,wpf中的ObservableCollection,MSDN中说,在添加项,移除项时此集合通知控件
,我们知道对一个集合的操作是CURD
但是Update的时候没有提供集合通知,也就是说当我Update的时候,虽然"集合内容“已被修改,但是"控件“却没有实现同步更新
即ObservableCollection,ObservableCollection类实现了INotifyPropertyChanged,所以集合“新增”、“修改”会通知,而“修改”,需要T实现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
mvvmlight的ViewModelBase已实现该方法,使用如下