zoukankan      html  css  js  c++  java
  • WPF 绑定各种数据源之LINQ检索结果

    LINQ查询的结果是一个IEnumberable<T>类型对象,而IEnumberable<T>类型对象有派生自IEnumberable,所以它可以作为列表控件的ItemsSource来使用.

    我创建了一个Sutdent类:

        public class Student
        {
            public int ID{ get; set;}
            public string Name { get; set; }
            public string Age { get; set; }
        }
    

    UI的设计如下:

      <ListView  Background="LightCoral"  HorizontalAlignment="Left" Margin="37,169,0,0" Name="listView1" Width="198" Height="100" VerticalAlignment="Top">
                <ListView.View>
                    <GridView>
                        <GridViewColumn DisplayMemberBinding="{Binding ID}" Header="ID"/>
                        <GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name"/>
                        <GridViewColumn DisplayMemberBinding="{Binding Age}" Header="Age"/>
                    </GridView>
                </ListView.View>
            </ListView>
    

    c#代码如下:

     List<Student> studentList = new List<Student>()
                {
                    new Student(){ID=10,Name="Cat", Age="39"},
                    new Student(){ID=10,Name="Cat1", Age="99"},
                    new Student(){ID=10,Name="Dog", Age="89"},
                    new Student(){ID=10,Name="Dog1", Age="29"},
                };
                listView1.ItemsSource = from stu in studentList where stu.Name.StartsWith("C") select stu;
    

    显示的结果是Name以"C"开头的数据,分别为"Cat"和"Cat1”.

    效果图如下:

                

     2、如果数据源已经是DataTable,则使用LINQ检索结果的形式为请参考: WPF 绑定各种数据源之Datatable 第三点(3、如果数据源已经是DataTable,则使用LINQ检索结果的形式为)。

  • 相关阅读:
    周学习笔记(04)——大三下
    进度(3)
    进度(2)
    进度(1)
    周学习笔记(03)——大三下
    《信息领域热词分析》之在代码层实现可用性战术
    周学习笔记(02)——大三下
    cf1041E
    cf1067b
    cf1131D
  • 原文地址:https://www.cnblogs.com/linlf03/p/2179698.html
Copyright © 2011-2022 走看看