使用Linq检索结果作为绑定源
来源:互联网 发布:激光雕刻切割机软件 编辑:程序博客网 时间:2024/06/05 06:32
本例介绍通过Linq检索三种重要绑定来源:
Model:
public class AStudent { public string Alias { get; set; } public string StuName { get; set; } }
前端Xaml:
<Window x:Class="Demo.BindingSrc2Linq" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="BindingSrc2Linq" Height="300" Width="300"> <Grid> <StackPanel Orientation="Vertical" Background="LightBlue"> <ListView x:Name="listViewStudents" Height="143" Margin="5"> <ListView.View> <GridView> <GridViewColumn Header="别名" Width="60" DisplayMemberBinding="{Binding Alias}"/> <GridViewColumn Header="学生名" Width="60" DisplayMemberBinding="{Binding StuName}"/> </GridView> </ListView.View> </ListView> <Button Content="Load From Linq" Name="btnLoadFromLinq" Click="btnLoadFromLinq_Click" Margin="0 5" Height="30"/> <Button Content="Load From Linq DataTable" Name="btnLoadFromDataTable" Click="btnLoadFromDataTable_Click" Margin="0 5" Height="30"/> <Button Content="Load From Linq Xml" Name="btnLoadFromXml" Click="btnLoadFromXml_Click" Margin="0 5" Height="30"/> </StackPanel> </Grid></Window>后台:
public partial class BindingSrc2Linq : Window { public BindingSrc2Linq() { InitializeComponent(); } private void btnLoadFromLinq_Click(object sender, RoutedEventArgs e) { List<AStudent> myStudents; myStudents = new List<AStudent>() { new AStudent(){StuName="fasdf",Alias="sd"}, new AStudent(){StuName="fasdf2",Alias="sd2"}, new AStudent(){StuName="gediek",Alias="gd"}, new AStudent(){StuName="Tom",Alias="tm2"}, new AStudent(){StuName="Tim",Alias="tm"}, new AStudent(){StuName="Teacher",Alias="tc"} }; listViewStudents.ItemsSource = myStudents; listViewStudents.ItemsSource = from stu in myStudents where stu.StuName.StartsWith("T") select stu; } private void btnLoadFromDataTable_Click(object sender, RoutedEventArgs e) { DataTable dt = new DataTable(); DataColumn dc = new DataColumn("StudentName"); DataColumn dc2 = new DataColumn("StudentAlias"); dt.Columns.Add(dc); dt.Columns.Add(dc2); DataRow dr = dt.NewRow(); dr["StudentName"] = "Hobby"; dr["StudentAlias"] = "Hb"; DataRow dr2 = dt.NewRow(); dr2["StudentName"] = "Slila"; dr2["StudentAlias"] = "Sl"; DataRow dr3 = dt.NewRow(); dr3["StudentName"] = "Hobby2"; dr3["StudentAlias"] = "Hb2"; dt.Rows.Add(dr); dt.Rows.Add(dr2); dt.Rows.Add(dr3); this.listViewStudents.ItemsSource = from row in dt.Rows.Cast<DataRow>() where Convert.ToString(row["StudentName"]).StartsWith("H") select new AStudent() { StuName = row["StudentName"].ToString(), Alias = row["StudentAlias"].ToString() }; } private void btnLoadFromXml_Click(object sender, RoutedEventArgs e) { XDocument xdoc = XDocument.Load("RawData.xml"); this.listViewStudents.ItemsSource = from element in xdoc.Descendants("Student")//注意此处是子节点名称. where element.Attribute("sname").Value.StartsWith("L") select new AStudent() { StuName = element.Attribute("sname").Value, Alias = element.Attribute("salias").Value }; } }
0 0
- 使用Linq检索结果作为绑定源
- WPF 使用LINQ 检索结果作为Binding源
- WPF中的binding(七)- 使用LINQ检索作为Binding的数据源
- 使用LINQ检索重复值
- mysql使用查询结果作为临时表
- [Sed] 使用替换结果作为命令执行
- 杂七杂八——使用LINQ检索重复值
- 在Linq中使用委托作为相等比较
- 使用Linq快速查找两个DataTable不同的结果
- 建议26:使用匿名类型存储LINQ查询结果
- 【DRP】ResultSet使用 getxxx() 方法检索查询结果
- c# comboBox 使用 linq to xml 绑定 XML文件
- [代码]使用LINQ的查询结果填充XML树(LINQ to XML)
- linq结果groupby
- shenlan211314 Google检索结果
- 检索结果聚类
- 将Thread对象作为Runnable对象使用的结果
- LINQ绑定TreeView
- 影响全球银行的APT目标攻击
- C/C++时间函数的使用
- Android判断网络是否连接
- iOS动画效果和实现
- Unity 3D之使用GameObject创建简单的可移动的对象
- 使用Linq检索结果作为绑定源
- 微信钱包支付提现多久能到账
- Apache Maven 入门篇 (一)
- Android的动画总结
- centos6快速部署java应用
- SPRING-MVC访问静态文件,如jpg,js,css
- 3.15打击假冒网站,就用 “沃通国际认证”
- Makefile伪目标
- 在非越狱设备上使用 LLDB 调试第三方 App