ons.Add(new Person("Tom", 8)); this.DataContext = this.persons; showButton.Click += showButton_Click; birthdayButton.Click += birthdayButton_Click; addPersonButton.Click += addPersonButton_Click; } void showButton_Click(object sender, ClickEventArgs e) { ListCollectionView view = (ListCollectionView)Binding.GetView(persons); Person person = (Person)view.CurrentItem.Current; MessageBox.Show( string.Format("Name is '{0}' and you are {1} years old", person.Name, person.Age)); } ... } 这个 Show 按钮单击处理程序代码调用 Binding 对象上的静态 GetView 方法,该对象会返回与 person 数据相关联的默认视图。回忆 persons 字段是 ArrayListDataCollection 的实例(您将会想到我的上一篇文章),它是 ArrayList 类的子类,该类添加 ICollectionChange 接口的实现,以便绑定到集合的控件(如 Perso页码:[1] [2] [3] [4] [5] [6] [7] [8] 第3页、共8页 |