zoukankan      html  css  js  c++  java
  • 【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/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    摄影/秋日花展(二)
    cordova android
    Java Web Services (2)
    7、索引
    洛谷——P1164 小A点菜
    洛谷——P1616 疯狂的采药
    洛谷——P1910 L国的战斗之间谍
    洛谷——P1095 守望者的逃离
    codevs——T1169 传纸条
    计算机硬件历史
  • 原文地址:https://www.cnblogs.com/zscmj/p/5958657.html
Copyright © 2011-2022 走看看