读书人

深入显出WPF 第二部分(4)

发布时间: 2012-12-18 12:43:41 作者: rapoo

深入浅出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];        }


读书人网 >软件开发

热点推荐