本文共 1053 字,大约阅读时间需要 3 分钟。
ItemsSource属性可以接收一个IEnumerable接口派生类的实例作为自己的值,ItemsSource里存放的是一条一条的数据,列表式控件的条目容器会为这些数据传上外衣,只要为ItemsControl对象设置了ItemsSource属性值,ItemsControl对象就会自动迭代其中的数据元素,为每一个数据元素准备一个条目容器,并使用Binding在条目容器与数据元素之间建立起关联,例子:
界面代码:
后台代码:
public MainWindow() { InitializeComponent(); ListstuList = new List () { new Student(){Id=0, Name="daijun", Age=11}, new Student(){Id=1, Name="Tim", Age=12}, new Student(){Id=2, Name="Tom", Age=13}, new Student(){Id=3, Name="Kyle", Age=14}, new Student(){Id=4, Name="Tony", Age=15} }; listBoxStudents.ItemsSource = stuList; listBoxStudents.DisplayMemberPath = "Name"; Binding binding = new Binding(); binding.Source = listBoxStudents; binding.Path = new PropertyPath("SelectedItem.Id"); textBox1.SetBinding(TextBox.TextProperty, binding); }
转载地址:http://khxym.baihongyu.com/