LINQ检索结果作为Binding的源----查询

该博客介绍了如何在C# WPF应用中使用ListView展示数据,包括从内存中的列表、DataTable对象和XML文件加载数据。通过创建Student类,设置UI布局,并在按钮点击事件中实现数据过滤和绑定。
摘要由CSDN通过智能技术生成

一、创建一个类

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

二、UI

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="220" Width="280" >
    <StackPanel Background="LightBlue">
        <ListView x:Name="listViewStudents" Height="143" Margin="5">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Id" Width="60" DisplayMemberBinding="{Binding Id}"/>
                    <GridViewColumn Header="Name" Width="100" DisplayMemberBinding="{Binding Name}"/>
                    <GridViewColumn Header="Age" Width="80" DisplayMemberBinding="{Binding Age}"/>
                </GridView>
            </ListView.View>
        </ListView>
        <Button Content="Load" Height="25" Margin="5,0" Click="Button_Click"/>
    </StackPanel>
</Window>

三、button事件

 private void Button_Click(object sender, RoutedEventArgs e)
        {
            List<Student> stuList = new List<Student>()
            {
                   new Student() { Id=0,Name="Tim",Age=29},
                   new Student() { Id=1,Name="Tom",Age=28},
                   new Student() { Id=2,Name="KY",Age=27},
                   new Student() { Id=3,Name="JON",Age=26},
                   new Student() { Id=4,Name="KEY",Age=25},
                   new Student() { Id=5,Name="APP",Age=24},
            };
            this.listViewStudents.ItemsSource = from stu in stuList where stu.Name.StartsWith("T") select stu;
        }

四、如果数据存放在一个已近填充好的DataTable对象里

C#填充datatable的两种方法_匹诺丶老曹的博客-CSDN博客

  private void Button_Click(object sender, RoutedEventArgs e)
        {
            DataTable dt = this.GetDataTable();
            this.listViewStudents.ItemsSource = from row in dt.Rows.Cast<DataRow>()
                                                where Convert.ToString(row[Name]).StartsWith("T")
                                                select new Student()
                                                {
                Id = int.Parse(row["Id"].ToString()),
                Name = row["Name"].ToString(),
                Age = int.Parse(row["Age"].ToString())
             };

        }

五、数据存储在XMl文件中(E:\RawData.xml)

RawData.xml

<?xml version="1.0" encoding="utf-8"?>
<StudentList>
	<Class>
		<Student Id="0" Name="Tim" Age="29"/>
	</Class>
</StudentList>

MainWindow.xmal.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace WpfApplication1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            XDocument xdoc = XDocument.Load(@"E:\RawData.xml");
            this.listViewStudents.ItemsSource =
                from element in xdoc.Descendants("Student")
                where element.Attribute("Name").Value.StartsWith("T")
                select new Student()
                {
                    Id = int.Parse(element.Attribute("Id").Value),
                    Name = element.Attribute("Name").Value,
                    Age=int.Parse(element.Attribute("Age").Value)

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值