一、创建一个类
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; }
}
}