Wpf binding 学习
2021-04-25 03:26
标签:inf rect add sig ble str book lda xpath 使用集合对象作为列表控件的ItemSource 前台: 后台: List { new Student(){StudentID=0,StudentName="崔一",StudentAge=27}, new Student(){StudentID=1,StudentName="沈二",StudentAge=27}, new Student(){StudentID=2,StudentName="张三",StudentAge=18}, new Student(){StudentID=3,StudentName="李四",StudentAge=19}, new Student(){StudentID=4,StudentName="王五",StudentAge=20}, }; this.item_List.ItemsSource = stuList; this.item_List.DisplayMemberPath = "StudentName"; Binding binding = new Binding("SelectedItem.StudentID") { Source = this.item_List }; this.tb_List.SetBinding(TextBox.TextProperty, binding); listBoxStudent.ItemsSource = stuList; 2.使用ADO.NET对象作为Binding对象 前台: 后台: DataTable dt = new DataTable(); DataColumn dc1 = new DataColumn("Id"); DataColumn dc2 = new DataColumn("Name"); DataColumn dc3 = new DataColumn("Age"); dt.Columns.Add(dc1); dt.Columns.Add(dc2); dt.Columns.Add(dc3); DataRow dr1 = dt.NewRow(); dr1["Id"] = "1"; dr1["Name"] = "Tim"; dr1["Age"] = "29"; dt.Rows.Add(dr1); DataRow dr2 = dt.NewRow(); dr2["Id"] = "2"; dr2["Name"] = "Tom"; dr2["Age"] = "28"; dt.Rows.Add(dr2); DataRow dr3= dt.NewRow(); dr3["Id"] = "3"; dr3["Name"] = "Tony"; dr3["Age"] = "27"; dt.Rows.Add(dr3); DataRow dr4 = dt.NewRow(); dr4["Id"] = "4"; dr4["Name"] = "Kyne"; dr4["Age"] = "26"; dt.Rows.Add(dr4); DataRow dr5 = dt.NewRow(); dr5["Id"] = "5"; dr5["Name"] = "Vina"; dr5["Age"] = "25"; dt.Rows.Add(dr5); DataRow dr6 = dt.NewRow(); dr6["Id"] = "6"; dr6["Name"] = "Emily"; dr6["Age"] = "24"; dt.Rows.Add(dr6); listStudentView.ItemsSource = dt.DefaultView; listStudentView.DisplayMemberPath = "Name"; 3. 使用Xml作为数据源绑定 前台: 后台: XmlDataProvider xdp = new XmlDataProvider(); xdp.Source = new Uri(AppDomain.CurrentDomain.BaseDirectory + "StudentInfo.xml"); xdp.XPath = @"/StudentList/Student"; XmlListView.DataContext = xdp; this.XmlListView.SetBinding(ListView.ItemsSourceProperty, new Binding()); Xml文件: 29 28 27 26 25 24 注意:Xpath=@Id和Xpath=Name的区别,使用@符号加字符串表示的是xml元素的Attribute,不加表示子级元素 将XML和XmlDataProvider写在window.resources中 如: 使用: 结果: 4. 使用ObjeckDataProvider对象最为Binding的source 代码: class Calculator { public string Add(string arg1, string arg2) { double x = 0; double y = 0; double z = 0; if (double.TryParse(arg1, out x) && double.TryParse(arg2, out y)) { z = x + y; return z.ToString(); } return "Input Error"; } } private void Button_Click(object sender, RoutedEventArgs e) { ObjectDataProvider odp = new ObjectDataProvider(); odp.ObjectInstance = new Calculator(); odp.MethodName = "Add"; odp.MethodParameters.Add("100"); odp.MethodParameters.Add("200"); MessageBox.Show(odp.Data.ToString()); } 5.Linq绑定数据源 Xml: 代码绑定: XDocument xdoc = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + @"XmlStudentInfo.xml"); this.ObjectStudentList.ItemsSource = from element in xdoc.Descendants("Student") where element.Attribute("Name").Value.StartsWith("T") select new Student() { StudentID = int.Parse(element.Attribute("Id").Value), StudentName = element.Attribute("Name").Value, StudentAge = int.Parse(element.Attribute("Age").Value) }; Wpf binding 学习 标签:inf rect add sig ble str book lda xpath 原文地址:http://www.cnblogs.com/BeeSnow/p/7922693.html