深入浅出WPF 第二部分(4)
6.3.7 使用集合对象作为列表控件的ItemsSource
public MainWindow() { InitializeComponent(); //Another bind source method //this.listViewStudents.ItemsSource=Load().DefaultView; this.listViewStudents.DataContext = Load(); this.listViewStudents.SetBinding(ListView.ItemsSourceProperty, new Binding()); this.textBoxId.SetBinding(TextBox.TextProperty, new Binding("SelectedItem.Id") { Source = listViewStudents, Mode = BindingMode.OneWay }); } private DataTable Load() { var connStr = "Data Source=(local);Initial Catalog=AndersDB;Integrated Security=True"; SqlCommand sqlComm = new SqlCommand("select * from Student", new SqlConnection(connStr)); SqlDataAdapter sqlDA = new SqlDataAdapter(sqlComm); DataSet ds = new DataSet(); sqlDA.Fill(ds); return ds.Tables[0]; }