【2016-10-13】【坚持学习】【Day4】【WPF】【ObservableCollection<T>】

今天在项目中使用到这个 ObservableCollection<T> 类,作为数据源集合绑定到控件。

当数据源发生变化,会通知界面显示。

如果用List<T> ,当数据源发生变化就得要重新设置ItemsSource,效率低下。

用ObservableCollection<T> 要注意的是,T必须继承 INotifyPropertyChanged

 

public class Student : INotifyPropertyChanged
    {
        private string studentID;
        public string StudentID
        {
            get { return studentID; }
            set
            {
                studentID = value;
                NotifyPropertyChange("StudentID");
            }
        }
        private string studentName;
        public string StudentName
        {
            get { return studentName; }
            set
            {
                studentName = value;
                NotifyPropertyChange("StudentName");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChange(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

 

posted @ 2016-10-13 23:32  zscmj  阅读(151)  评论(0编辑  收藏  举报