使用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