【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));
            }
        }

作者:zscmj
出处:http://www.cnblogs.com/zscmj/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

原文地址:https://www.cnblogs.com/zscmj/p/5958657.html