publicclassPerson { publicstring? Name { get; set; } publicstring? Home { get; set; } }
創(chuàng)建Person對象列表:
// 創(chuàng)建一個Person對象的列表 List<Person> people = new List<Person>() { new Person {Name = "張三",Home = "武漢" }, new Person {Name = "李四",Home = "南昌" }, new Person {Name = "王五",Home = "福州" }, };
publicstring Name { get { return _name; } set { _name = value; // Call OnPropertyChanged whenever the property is updated OnPropertyChanged("Name"); } } privatestring? _home;
publicstring Home { get { return _home; } set { _home = value; // Call OnPropertyChanged whenever the property is updated OnPropertyChanged("Home"); } }
// Declare the event publicevent PropertyChangedEventHandler? PropertyChanged; // Create the OnPropertyChanged method to raise the event protectedvoidOnPropertyChanged(string name) { var handler = PropertyChanged; handler?.Invoke(this, new PropertyChangedEventArgs(name)); } }
實現(xiàn)了INotifyPropertyChanged接口。
創(chuàng)建數(shù)據(jù)源:
// 創(chuàng)建一個Student對象的列表 BindingList<Student> students = new BindingList<Student>() { new Student { Name = "張三", Home = "武漢" }, new Student { Name = "李四", Home = "南昌" }, new Student { Name = "王五", Home = "福州" }, };