翼度科技»论坛 编程开发 .net 查看内容

浅谈WPF之DataGrid过滤,分组,排序

2

主题

2

帖子

6

积分

新手上路

Rank: 1

积分
6
使用过Excel的用户都知道,Excel可以方便的对数据进行分组,过滤,排序等操作,而在WPF中,默认提供的DataGrid只有很简单的功能,那么如何才能让我们开发的DataGrid,也像Excel一样具备丰富的客户端操作呢?今天就以一个简单的小例子,简述如何在WPF中实现DataGrid的过滤,筛选,排序等功能。仅供学习分享使用,如有不足之处,还请指正。

涉及知识点

在本示例中,涉及知识点如下所示:

  • CollectionView, CollectionView 类为实现 IEnumerable 接口的数据源提供分组和排序功能。
  • CollectionViewSource,CollectionViewSource 类允许你从 XAML 设置 CollectionView 的属性。
注意:此两个类,是我们实现客户端过滤,分组,排序的关键。
 
普通绑定

 
1. 构建数据源

 
在WPF中,DataGrid的ItemSource属性用于绑定数据源,而数据源必须是实现IEnumerable接口的的列表类型,在本示例中,采用具有通知属性的列表类型ObservableCollection。当列表中元素数量发生变化时,可以实时的通知DataGrid进行刷新。
1.1 创建实体

在本示例中,为了测试,创建Student实体模型,如下所示:
  1. public class Student
  2. {
  3. <Window.Resources>
  4.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  5. </Window.Resources>public string No { get; set; }
  6. <Window.Resources>
  7.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  8. </Window.Resources>public string Name { get; set; }
  9. <Window.Resources>
  10.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  11. </Window.Resources>public int Age { get; set; }
  12. <Window.Resources>
  13.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  14. </Window.Resources>public bool Sex { get; set; }
  15. <Window.Resources>
  16.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  17. </Window.Resources>public string Class { get; set; }
  18. }
复制代码
1.2 初始化数据源列表

在本示例采用MVVM模式开发,在ViewModel中创建ObservableCollection类型的Students列表,如下所示:
  1. using CommunityToolkit.Mvvm.ComponentModel;
  2. using DemoDataGrid2.Models;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace DemoDataGrid2.ViewModels
  10. {
  11. <Window.Resources>
  12.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  13. </Window.Resources>public class TestWindowViewModel:ObservableObject
  14. <Window.Resources>
  15.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  16. </Window.Resources>{
  17. <Window.Resources>
  18.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  19. </Window.Resources><Window.Resources>
  20.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  21. </Window.Resources>private ObservableCollection<Student> students;
  22. <Window.Resources>
  23.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  24. </Window.Resources><Window.Resources>
  25.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  26. </Window.Resources>public ObservableCollection<Student> Students
  27. <Window.Resources>
  28.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  29. </Window.Resources><Window.Resources>
  30.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  31. </Window.Resources>{
  32. <Window.Resources>
  33.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  34. </Window.Resources><Window.Resources>
  35.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  36. </Window.Resources><Window.Resources>
  37.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  38. </Window.Resources>get { return students; }
  39. <Window.Resources>
  40.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  41. </Window.Resources><Window.Resources>
  42.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  43. </Window.Resources><Window.Resources>
  44.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  45. </Window.Resources>set { SetProperty(ref students, value); }
  46. <Window.Resources>
  47.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  48. </Window.Resources><Window.Resources>
  49.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  50. </Window.Resources>}
  51. <Window.Resources>
  52.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  53. </Window.Resources><Window.Resources>
  54.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  55. </Window.Resources>public TestWindowViewModel()
  56. <Window.Resources>
  57.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  58. </Window.Resources><Window.Resources>
  59.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  60. </Window.Resources>{
  61. <Window.Resources>
  62.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  63. </Window.Resources><Window.Resources>
  64.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  65. </Window.Resources><Window.Resources>
  66.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  67. </Window.Resources>var parentName = new string[5] { "张", "王", "李", "赵", "刘" };
  68. <Window.Resources>
  69.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  70. </Window.Resources><Window.Resources>
  71.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  72. </Window.Resources><Window.Resources>
  73.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  74. </Window.Resources>this.Students = new ObservableCollection<Student>();
  75. <Window.Resources>
  76.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  77. </Window.Resources><Window.Resources>
  78.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  79. </Window.Resources><Window.Resources>
  80.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  81. </Window.Resources>for (int i = 0; i < 100; i++)
  82. <Window.Resources>
  83.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  84. </Window.Resources><Window.Resources>
  85.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  86. </Window.Resources><Window.Resources>
  87.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  88. </Window.Resources>{
  89. <Window.Resources>
  90.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  91. </Window.Resources><Window.Resources>
  92.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  93. </Window.Resources><Window.Resources>
  94.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  95. </Window.Resources><Window.Resources>
  96.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  97. </Window.Resources>Student student = new Student();
  98. <Window.Resources>
  99.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  100. </Window.Resources><Window.Resources>
  101.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  102. </Window.Resources><Window.Resources>
  103.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  104. </Window.Resources><Window.Resources>
  105.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  106. </Window.Resources>student.No = i.ToString().PadLeft(3, '0');
  107. <Window.Resources>
  108.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  109. </Window.Resources><Window.Resources>
  110.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  111. </Window.Resources><Window.Resources>
  112.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  113. </Window.Resources><Window.Resources>
  114.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  115. </Window.Resources>student.Name = parentName[(i % 4)] + i.ToString().PadLeft(2, 'A');
  116. <Window.Resources>
  117.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  118. </Window.Resources><Window.Resources>
  119.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  120. </Window.Resources><Window.Resources>
  121.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  122. </Window.Resources><Window.Resources>
  123.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  124. </Window.Resources>student.Age = 20 + (i % 5);
  125. <Window.Resources>
  126.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  127. </Window.Resources><Window.Resources>
  128.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  129. </Window.Resources><Window.Resources>
  130.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  131. </Window.Resources><Window.Resources>
  132.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  133. </Window.Resources>student.Sex = i % 2 == 0 ? true : false;
  134. <Window.Resources>
  135.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  136. </Window.Resources><Window.Resources>
  137.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  138. </Window.Resources><Window.Resources>
  139.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  140. </Window.Resources><Window.Resources>
  141.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  142. </Window.Resources>student.Class = $"{(i % 3)}班";
  143. <Window.Resources>
  144.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  145. </Window.Resources><Window.Resources>
  146.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  147. </Window.Resources><Window.Resources>
  148.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  149. </Window.Resources><Window.Resources>
  150.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  151. </Window.Resources>this.Students.Add(student);
  152. <Window.Resources>
  153.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  154. </Window.Resources><Window.Resources>
  155.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  156. </Window.Resources><Window.Resources>
  157.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  158. </Window.Resources>}
  159. <Window.Resources>
  160.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  161. </Window.Resources><Window.Resources>
  162.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  163. </Window.Resources>}
  164. <Window.Resources>
  165.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  166. </Window.Resources>}
  167. }
复制代码
注意:构造函数中的方法,用于创建Students列表,包含100名学生,分别对应不同的编号,姓名,年龄,性别,班级等信息。
2. 页面绑定

在ViewModel中创建数据源后,可以在Xaml中进行绑定【语法:ItemsSource="{Binding Students}"】,如下所示:
  1. <Window x:
  2. <Window.Resources>
  3.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  4. </Window.Resources><Window.Resources>
  5.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  6. </Window.Resources>xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  7. <Window.Resources>
  8.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  9. </Window.Resources><Window.Resources>
  10.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  11. </Window.Resources>xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  12. <Window.Resources>
  13.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  14. </Window.Resources><Window.Resources>
  15.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  16. </Window.Resources>xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  17. <Window.Resources>
  18.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  19. </Window.Resources><Window.Resources>
  20.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  21. </Window.Resources>xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  22. <Window.Resources>
  23.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  24. </Window.Resources><Window.Resources>
  25.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  26. </Window.Resources>xmlns:local="clr-namespace:DemoDataGrid2"
  27. <Window.Resources>
  28.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  29. </Window.Resources><Window.Resources>
  30.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  31. </Window.Resources>mc:Ignorable="d"
  32. <Window.Resources>
  33.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  34. </Window.Resources><Window.Resources>
  35.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  36. </Window.Resources>Title="DataGrid示例" Height="450" Width="800">
  37. <Window.Resources>
  38.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  39. </Window.Resources><Grid>
  40. <Window.Resources>
  41.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  42. </Window.Resources><Window.Resources>
  43.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  44. </Window.Resources><Grid.RowDefinitions>
  45. <Window.Resources>
  46.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  47. </Window.Resources><Window.Resources>
  48.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  49. </Window.Resources><Window.Resources>
  50.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  51. </Window.Resources><RowDefinition Height="Auto"></RowDefinition>
  52. <Window.Resources>
  53.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  54. </Window.Resources><Window.Resources>
  55.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  56. </Window.Resources><Window.Resources>
  57.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  58. </Window.Resources><RowDefinition></RowDefinition>
  59. <Window.Resources>
  60.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  61. </Window.Resources><Window.Resources>
  62.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  63. </Window.Resources></Grid.RowDefinitions>
  64. <Window.Resources>
  65.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  66. </Window.Resources><Window.Resources>
  67.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  68. </Window.Resources><DockPanel Grid.Row="0">
  69. <Window.Resources>
  70.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  71. </Window.Resources><Window.Resources>
  72.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  73. </Window.Resources><Window.Resources>
  74.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  75. </Window.Resources>
  76. <Window.Resources>
  77.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  78. </Window.Resources><Window.Resources>
  79.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  80. </Window.Resources></DockPanel>
  81. <Window.Resources>
  82.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  83. </Window.Resources><Window.Resources>
  84.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  85. </Window.Resources><DataGrid Grid.Row="1" ItemsSource="{Binding Students}" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" >
  86. <Window.Resources>
  87.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  88. </Window.Resources><Window.Resources>
  89.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  90. </Window.Resources><Window.Resources>
  91.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  92. </Window.Resources><DataGrid.Columns>
  93. <Window.Resources>
  94.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  95. </Window.Resources><Window.Resources>
  96.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  97. </Window.Resources><Window.Resources>
  98.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  99. </Window.Resources><Window.Resources>
  100.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  101. </Window.Resources><DataGridTextColumn Header="学号" Binding="{Binding No}" Width="*"></DataGridTextColumn>
  102. <Window.Resources>
  103.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  104. </Window.Resources><Window.Resources>
  105.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  106. </Window.Resources><Window.Resources>
  107.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  108. </Window.Resources><Window.Resources>
  109.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  110. </Window.Resources><DataGridTextColumn Header="姓名" Binding="{Binding Name}"  Width="*"></DataGridTextColumn>
  111. <Window.Resources>
  112.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  113. </Window.Resources><Window.Resources>
  114.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  115. </Window.Resources><Window.Resources>
  116.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  117. </Window.Resources><Window.Resources>
  118.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  119. </Window.Resources><DataGridTextColumn Header="年龄" Binding="{Binding Age}"  Width="*"></DataGridTextColumn>
  120. <Window.Resources>
  121.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  122. </Window.Resources><Window.Resources>
  123.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  124. </Window.Resources><Window.Resources>
  125.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  126. </Window.Resources><Window.Resources>
  127.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  128. </Window.Resources><DataGridTemplateColumn Header="性别"  Width="*">
  129. <Window.Resources>
  130.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  131. </Window.Resources><Window.Resources>
  132.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  133. </Window.Resources><Window.Resources>
  134.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  135. </Window.Resources><Window.Resources>
  136.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  137. </Window.Resources><Window.Resources>
  138.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  139. </Window.Resources><DataGridTemplateColumn.CellTemplate>
  140. <Window.Resources>
  141.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  142. </Window.Resources><Window.Resources>
  143.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  144. </Window.Resources><Window.Resources>
  145.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  146. </Window.Resources><Window.Resources>
  147.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  148. </Window.Resources><Window.Resources>
  149.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  150. </Window.Resources><Window.Resources>
  151.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  152. </Window.Resources><DataTemplate>
  153. <Window.Resources>
  154.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  155. </Window.Resources><Window.Resources>
  156.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  157. </Window.Resources><Window.Resources>
  158.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  159. </Window.Resources><Window.Resources>
  160.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  161. </Window.Resources><Window.Resources>
  162.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  163. </Window.Resources><Window.Resources>
  164.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  165. </Window.Resources><Window.Resources>
  166.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  167. </Window.Resources><TextBlock x:Name="sex">
  168. <Window.Resources>
  169.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  170. </Window.Resources><Window.Resources>
  171.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  172. </Window.Resources><Window.Resources>
  173.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  174. </Window.Resources><Window.Resources>
  175.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  176. </Window.Resources><Window.Resources>
  177.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  178. </Window.Resources><Window.Resources>
  179.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  180. </Window.Resources><Window.Resources>
  181.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  182. </Window.Resources><Window.Resources>
  183.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  184. </Window.Resources><TextBlock.Style>
  185. <Window.Resources>
  186.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  187. </Window.Resources><Window.Resources>
  188.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  189. </Window.Resources><Window.Resources>
  190.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  191. </Window.Resources><Window.Resources>
  192.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  193. </Window.Resources><Window.Resources>
  194.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  195. </Window.Resources><Window.Resources>
  196.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  197. </Window.Resources><Window.Resources>
  198.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  199. </Window.Resources><Window.Resources>
  200.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  201. </Window.Resources><Window.Resources>
  202.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  203. </Window.Resources>
  204. <Window.Resources>
  205.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  206. </Window.Resources><Window.Resources>
  207.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  208. </Window.Resources><Window.Resources>
  209.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  210. </Window.Resources><Window.Resources>
  211.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  212. </Window.Resources><Window.Resources>
  213.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  214. </Window.Resources><Window.Resources>
  215.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  216. </Window.Resources><Window.Resources>
  217.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  218. </Window.Resources><Window.Resources>
  219.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  220. </Window.Resources></TextBlock.Style>
  221. <Window.Resources>
  222.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  223. </Window.Resources><Window.Resources>
  224.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  225. </Window.Resources><Window.Resources>
  226.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  227. </Window.Resources><Window.Resources>
  228.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  229. </Window.Resources><Window.Resources>
  230.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  231. </Window.Resources><Window.Resources>
  232.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  233. </Window.Resources><Window.Resources>
  234.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  235. </Window.Resources></TextBlock>
  236. <Window.Resources>
  237.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  238. </Window.Resources><Window.Resources>
  239.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  240. </Window.Resources><Window.Resources>
  241.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  242. </Window.Resources><Window.Resources>
  243.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  244. </Window.Resources><Window.Resources>
  245.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  246. </Window.Resources><Window.Resources>
  247.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  248. </Window.Resources></DataTemplate>
  249. <Window.Resources>
  250.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  251. </Window.Resources><Window.Resources>
  252.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  253. </Window.Resources><Window.Resources>
  254.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  255. </Window.Resources><Window.Resources>
  256.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  257. </Window.Resources><Window.Resources>
  258.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  259. </Window.Resources></DataGridTemplateColumn.CellTemplate>
  260. <Window.Resources>
  261.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  262. </Window.Resources><Window.Resources>
  263.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  264. </Window.Resources><Window.Resources>
  265.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  266. </Window.Resources><Window.Resources>
  267.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  268. </Window.Resources></DataGridTemplateColumn>
  269. <Window.Resources>
  270.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  271. </Window.Resources><Window.Resources>
  272.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  273. </Window.Resources><Window.Resources>
  274.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  275. </Window.Resources><Window.Resources>
  276.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  277. </Window.Resources><DataGridTextColumn Header="班级" Binding="{Binding Class}"  Width="*"></DataGridTextColumn>
  278. <Window.Resources>
  279.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  280. </Window.Resources><Window.Resources>
  281.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  282. </Window.Resources><Window.Resources>
  283.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  284. </Window.Resources></DataGrid.Columns>
  285. <Window.Resources>
  286.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  287. </Window.Resources><Window.Resources>
  288.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  289. </Window.Resources></DataGrid>
  290. <Window.Resources>
  291.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  292. </Window.Resources></Grid>
  293. </Window>
复制代码
以下两点需要注意:

  • 在本示例中,性别为bool类型,要转换成汉字,用到了DataTrigger。
  • DataGrid的列可以自动生成,也可以手动创建,可以通过AutoGenerateColumns="False"来设置。
3. 普通绑定示例

普通绑定示例截图,如下所示:


 
DataGrid过滤

在DataGrid中,实现客户端过滤,且不需要重新初始化数据源,则需要用到CollectionViewSource。
1. 定义资源及绑定

将CollectionViewSource定义成一种资源,并将资源的Source属性绑定到数据源,再将DataGrid中的ItemSource绑定到此资源,然后就可以在过滤时对资源进行过滤。
定义资源如下所示:
  1. <Window.Resources>
  2.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  3. </Window.Resources>
复制代码
DataGrid绑定资源【语法:ItemsSource="{Binding Source={StaticResource cvStudents}}"】,如下所示:
  1. <Window.Resources>
  2.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  3. </Window.Resources><Window.Resources>
  4.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  5. </Window.Resources><Window.Resources>
  6.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  7. </Window.Resources><Window.Resources>
  8.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  9. </Window.Resources><Window.Resources>
  10.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  11. </Window.Resources><Window.Resources>
  12.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  13. </Window.Resources><Window.Resources>
  14.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  15. </Window.Resources><Window.Resources>
  16.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  17. </Window.Resources><Window.Resources>
  18.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  19. </Window.Resources><Window.Resources>
  20.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  21. </Window.Resources><Window.Resources>
  22.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  23. </Window.Resources><Window.Resources>
  24.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  25. </Window.Resources><Window.Resources>
  26.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  27. </Window.Resources><Window.Resources>
  28.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  29. </Window.Resources><Window.Resources>
  30.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  31. </Window.Resources><Window.Resources>
  32.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  33. </Window.Resources><Window.Resources>
  34.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  35. </Window.Resources><Window.Resources>
  36.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  37. </Window.Resources><Window.Resources>
  38.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  39. </Window.Resources><Window.Resources>
  40.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  41. </Window.Resources><Window.Resources>
  42.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  43. </Window.Resources><Window.Resources>
  44.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  45. </Window.Resources><Window.Resources>
  46.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  47. </Window.Resources><Window.Resources>
  48.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  49. </Window.Resources><Window.Resources>
  50.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  51. </Window.Resources><Window.Resources>
  52.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  53. </Window.Resources><Window.Resources>
  54.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  55. </Window.Resources><Window.Resources>
  56.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  57. </Window.Resources><Window.Resources>
  58.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  59. </Window.Resources><Window.Resources>
  60.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  61. </Window.Resources><Window.Resources>
  62.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  63. </Window.Resources><Window.Resources>
  64.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  65. </Window.Resources><Window.Resources>
  66.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  67. </Window.Resources><Window.Resources>
  68.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  69. </Window.Resources><Window.Resources>
  70.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  71. </Window.Resources><Window.Resources>
  72.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  73. </Window.Resources><Window.Resources>
  74.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  75. </Window.Resources><Window.Resources>
  76.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  77. </Window.Resources><Window.Resources>
  78.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  79. </Window.Resources><Window.Resources>
  80.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  81. </Window.Resources><Window.Resources>
  82.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  83. </Window.Resources><Window.Resources>
  84.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  85. </Window.Resources><Window.Resources>
  86.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  87. </Window.Resources><Window.Resources>
  88.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  89. </Window.Resources><Window.Resources>
  90.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  91. </Window.Resources><Window.Resources>
  92.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  93. </Window.Resources><Window.Resources>
  94.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  95. </Window.Resources><Window.Resources>
  96.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  97. </Window.Resources><Window.Resources>
  98.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  99. </Window.Resources><Window.Resources>
  100.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  101. </Window.Resources><Window.Resources>
  102.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  103. </Window.Resources><Window.Resources>
  104.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  105. </Window.Resources><Window.Resources>
  106.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  107. </Window.Resources><Window.Resources>
  108.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  109. </Window.Resources><Window.Resources>
  110.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  111. </Window.Resources><Window.Resources>
  112.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  113. </Window.Resources><Window.Resources>
  114.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  115. </Window.Resources>
复制代码
2. 过滤条件

在本示例中,以性别为过滤条件,当点击过滤条件时,触发过滤命令,如下所示:
  1. <Window.Resources>
  2.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  3. </Window.Resources><Window.Resources>
  4.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  5. </Window.Resources><Window.Resources>
  6.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  7. </Window.Resources><Window.Resources>
  8.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  9. </Window.Resources>
复制代码
当用户点击时,触发Command绑定的命令,如下所示:
  1. private ICommand fiterSexCheckedCommand;
  2. public ICommand FiterSexCheckedCommand
  3. {
  4.         get
  5.         {
  6.                 if (fiterSexCheckedCommand == null)
  7.                 {
  8.                         fiterSexCheckedCommand = new RelayCommand<object>(FilterSexChecked);
  9.                 }
  10.                 return fiterSexCheckedCommand;
  11.         }
  12. }
  13. private void FilterSexChecked(object obj)
  14. {
  15.         if (this.dataGrid != null)
  16.         {
  17.                 ICollectionView cvs = CollectionViewSource.GetDefaultView(this.dataGrid.ItemsSource);
  18.                 if (cvs != null && cvs.CanFilter)
  19.                 {
  20.                         cvs.Filter = (object obj) =>
  21.                         {
  22.                                 bool flag = true;
  23.                                 bool flag1 = true;
  24.                                 bool flag2 = true;
  25.                                 var student = obj as Student;
  26.                                 if (!FilterM.IsMaleChecked)
  27.                                 {
  28.                                         flag1 = student.Sex != true;
  29.                                 }
  30.                                 if (!FilterM.IsFemaleChecked)
  31.                                 {
  32.                                         flag2 = student.Sex != false;
  33.                                 }
  34.                                 flag = flag1 && flag2;
  35.                                 return flag;
  36.                         };
  37.                 }
  38.         }
  39. }
复制代码
注意:通过CollectionViewSource.GetDefaultView(this.dataGrid.ItemsSource)方法获取具有过滤功能的CollectionView类对象,然后再对Filter进行委托即可。
其中FilterM是在ViewModel中声明的FilterConditionM类型的属性。
  1. private FilterConditionM filterM;
  2. public FilterConditionM FilterM
  3. {
  4.         get { return filterM; }
  5.         set { SetProperty(ref filterM, value); }
  6. }
复制代码
 FilterConditionM是封装的过滤条件模型类, 如下所示:
  1. namespace DemoDataGrid2.Models{<Window.Resources>
  2.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  3. </Window.Resources>public class FilterConditionM:ObservableObject<Window.Resources>
  4.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  5. </Window.Resources>{                private bool isMaleChecked;                public bool IsMaleChecked                {                        get { return isMaleChecked; }                        set { SetProperty(ref isMaleChecked , value); }                }<Window.Resources>
  6.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  7. </Window.Resources><Window.Resources>
  8.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  9. </Window.Resources>private bool isFemaleChecked;<Window.Resources>
  10.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  11. </Window.Resources><Window.Resources>
  12.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  13. </Window.Resources>public bool IsFemaleChecked<Window.Resources>
  14.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  15. </Window.Resources><Window.Resources>
  16.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  17. </Window.Resources>{<Window.Resources>
  18.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  19. </Window.Resources><Window.Resources>
  20.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  21. </Window.Resources><Window.Resources>
  22.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  23. </Window.Resources>get { return isFemaleChecked; }<Window.Resources>
  24.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  25. </Window.Resources><Window.Resources>
  26.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  27. </Window.Resources><Window.Resources>
  28.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  29. </Window.Resources>set { SetProperty(ref isFemaleChecked, value); }<Window.Resources>
  30.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  31. </Window.Resources><Window.Resources>
  32.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  33. </Window.Resources>}<Window.Resources>
  34.     <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}"></CollectionViewSource>
  35. </Window.Resources>}}
复制代码
3. 过滤功能示例

具备过滤功能的示例截图,如下所示:


 
DataGrid分组

在WPF中,实现DataGrid的分组,也是通过CollectionViewSource来实现。
1. 设置分组列

有两种方式可以设置分组
1.1 XAML中设置

在XAML中,通过设置CollectionViewSource的GroupDescriptions属性,来设置具体分组的列属性,如下所示:
  1. <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}">
  2.         <CollectionViewSource.GroupDescriptions>
  3.                 <PropertyGroupDescription PropertyName="Class"/>
  4.                 <PropertyGroupDescription PropertyName="Sex"/>
  5.         </CollectionViewSource.GroupDescriptions>
  6. </CollectionViewSource>
复制代码
1.2 后台代码设置

在ViewModel中设置CollectionView的GroupDescriptions属性,如下所示:
  1. ICollectionView cvTasks = CollectionViewSource.GetDefaultView(this.dataGrid.ItemsSource);
  2. if (cvTasks != null && cvTasks.CanGroup == true)
  3. {
  4.         cvTasks.GroupDescriptions.Clear();
  5.         cvTasks.GroupDescriptions.Add(new PropertyGroupDescription("Class"));
  6.         cvTasks.GroupDescriptions.Add(new PropertyGroupDescription("Sex"));
  7. }
复制代码
2. 设置分组样式

在WPF中,通过设置DataGrid的GroupStyle属性来改变分组样式,如下所示:
  1. <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}">
  2.         <CollectionViewSource.GroupDescriptions>
  3.                 <PropertyGroupDescription PropertyName="Class"/>
  4.                 <PropertyGroupDescription PropertyName="Sex"/>
  5.         </CollectionViewSource.GroupDescriptions>
  6. </CollectionViewSource><CollectionViewSource x:Key="cvStudents" Source="{Binding Students}">
  7.         <CollectionViewSource.GroupDescriptions>
  8.                 <PropertyGroupDescription PropertyName="Class"/>
  9.                 <PropertyGroupDescription PropertyName="Sex"/>
  10.         </CollectionViewSource.GroupDescriptions>
  11. </CollectionViewSource><CollectionViewSource x:Key="cvStudents" Source="{Binding Students}">
  12.         <CollectionViewSource.GroupDescriptions>
  13.                 <PropertyGroupDescription PropertyName="Class"/>
  14.                 <PropertyGroupDescription PropertyName="Sex"/>
  15.         </CollectionViewSource.GroupDescriptions>
  16. </CollectionViewSource><CollectionViewSource x:Key="cvStudents" Source="{Binding Students}">
  17.         <CollectionViewSource.GroupDescriptions>
  18.                 <PropertyGroupDescription PropertyName="Class"/>
  19.                 <PropertyGroupDescription PropertyName="Sex"/>
  20.         </CollectionViewSource.GroupDescriptions>
  21. </CollectionViewSource><CollectionViewSource x:Key="cvStudents" Source="{Binding Students}">
  22.         <CollectionViewSource.GroupDescriptions>
  23.                 <PropertyGroupDescription PropertyName="Class"/>
  24.                 <PropertyGroupDescription PropertyName="Sex"/>
  25.         </CollectionViewSource.GroupDescriptions>
  26. </CollectionViewSource><CollectionViewSource x:Key="cvStudents" Source="{Binding Students}">
  27.         <CollectionViewSource.GroupDescriptions>
  28.                 <PropertyGroupDescription PropertyName="Class"/>
  29.                 <PropertyGroupDescription PropertyName="Sex"/>
  30.         </CollectionViewSource.GroupDescriptions>
  31. </CollectionViewSource><CollectionViewSource x:Key="cvStudents" Source="{Binding Students}">
  32.         <CollectionViewSource.GroupDescriptions>
  33.                 <PropertyGroupDescription PropertyName="Class"/>
  34.                 <PropertyGroupDescription PropertyName="Sex"/>
  35.         </CollectionViewSource.GroupDescriptions>
  36. </CollectionViewSource><CollectionViewSource x:Key="cvStudents" Source="{Binding Students}">
  37.         <CollectionViewSource.GroupDescriptions>
  38.                 <PropertyGroupDescription PropertyName="Class"/>
  39.                 <PropertyGroupDescription PropertyName="Sex"/>
  40.         </CollectionViewSource.GroupDescriptions>
  41. </CollectionViewSource><CollectionViewSource x:Key="cvStudents" Source="{Binding Students}">
  42.         <CollectionViewSource.GroupDescriptions>
  43.                 <PropertyGroupDescription PropertyName="Class"/>
  44.                 <PropertyGroupDescription PropertyName="Sex"/>
  45.         </CollectionViewSource.GroupDescriptions>
  46. </CollectionViewSource><CollectionViewSource x:Key="cvStudents" Source="{Binding Students}">
  47.         <CollectionViewSource.GroupDescriptions>
  48.                 <PropertyGroupDescription PropertyName="Class"/>
  49.                 <PropertyGroupDescription PropertyName="Sex"/>
  50.         </CollectionViewSource.GroupDescriptions>
  51. </CollectionViewSource><CollectionViewSource x:Key="cvStudents" Source="{Binding Students}">
  52.         <CollectionViewSource.GroupDescriptions>
  53.                 <PropertyGroupDescription PropertyName="Class"/>
  54.                 <PropertyGroupDescription PropertyName="Sex"/>
  55.         </CollectionViewSource.GroupDescriptions>
  56. </CollectionViewSource><CollectionViewSource x:Key="cvStudents" Source="{Binding Students}">
  57.         <CollectionViewSource.GroupDescriptions>
  58.                 <PropertyGroupDescription PropertyName="Class"/>
  59.                 <PropertyGroupDescription PropertyName="Sex"/>
  60.         </CollectionViewSource.GroupDescriptions>
  61. </CollectionViewSource><CollectionViewSource x:Key="cvStudents" Source="{Binding Students}">
  62.         <CollectionViewSource.GroupDescriptions>
  63.                 <PropertyGroupDescription PropertyName="Class"/>
  64.                 <PropertyGroupDescription PropertyName="Sex"/>
  65.         </CollectionViewSource.GroupDescriptions>
  66. </CollectionViewSource><CollectionViewSource x:Key="cvStudents" Source="{Binding Students}">
  67.         <CollectionViewSource.GroupDescriptions>
  68.                 <PropertyGroupDescription PropertyName="Class"/>
  69.                 <PropertyGroupDescription PropertyName="Sex"/>
  70.         </CollectionViewSource.GroupDescriptions>
  71. </CollectionViewSource><CollectionViewSource x:Key="cvStudents" Source="{Binding Students}">
  72.         <CollectionViewSource.GroupDescriptions>
  73.                 <PropertyGroupDescription PropertyName="Class"/>
  74.                 <PropertyGroupDescription PropertyName="Sex"/>
  75.         </CollectionViewSource.GroupDescriptions>
  76. </CollectionViewSource><CollectionViewSource x:Key="cvStudents" Source="{Binding Students}">
  77.         <CollectionViewSource.GroupDescriptions>
  78.                 <PropertyGroupDescription PropertyName="Class"/>
  79.                 <PropertyGroupDescription PropertyName="Sex"/>
  80.         </CollectionViewSource.GroupDescriptions>
  81. </CollectionViewSource><CollectionViewSource x:Key="cvStudents" Source="{Binding Students}">
  82.         <CollectionViewSource.GroupDescriptions>
  83.                 <PropertyGroupDescription PropertyName="Class"/>
  84.                 <PropertyGroupDescription PropertyName="Sex"/>
  85.         </CollectionViewSource.GroupDescriptions>
  86. </CollectionViewSource><CollectionViewSource x:Key="cvStudents" Source="{Binding Students}">
  87.         <CollectionViewSource.GroupDescriptions>
  88.                 <PropertyGroupDescription PropertyName="Class"/>
  89.                 <PropertyGroupDescription PropertyName="Sex"/>
  90.         </CollectionViewSource.GroupDescriptions>
  91. </CollectionViewSource><CollectionViewSource x:Key="cvStudents" Source="{Binding Students}">
  92.         <CollectionViewSource.GroupDescriptions>
  93.                 <PropertyGroupDescription PropertyName="Class"/>
  94.                 <PropertyGroupDescription PropertyName="Sex"/>
  95.         </CollectionViewSource.GroupDescriptions>
  96. </CollectionViewSource><CollectionViewSource x:Key="cvStudents" Source="{Binding Students}">
  97.         <CollectionViewSource.GroupDescriptions>
  98.                 <PropertyGroupDescription PropertyName="Class"/>
  99.                 <PropertyGroupDescription PropertyName="Sex"/>
  100.         </CollectionViewSource.GroupDescriptions>
  101. </CollectionViewSource><CollectionViewSource x:Key="cvStudents" Source="{Binding Students}">
  102.         <CollectionViewSource.GroupDescriptions>
  103.                 <PropertyGroupDescription PropertyName="Class"/>
  104.                 <PropertyGroupDescription PropertyName="Sex"/>
  105.         </CollectionViewSource.GroupDescriptions>
  106. </CollectionViewSource><CollectionViewSource x:Key="cvStudents" Source="{Binding Students}">
  107.         <CollectionViewSource.GroupDescriptions>
  108.                 <PropertyGroupDescription PropertyName="Class"/>
  109.                 <PropertyGroupDescription PropertyName="Sex"/>
  110.         </CollectionViewSource.GroupDescriptions>
  111. </CollectionViewSource><CollectionViewSource x:Key="cvStudents" Source="{Binding Students}">
  112.         <CollectionViewSource.GroupDescriptions>
  113.                 <PropertyGroupDescription PropertyName="Class"/>
  114.                 <PropertyGroupDescription PropertyName="Sex"/>
  115.         </CollectionViewSource.GroupDescriptions>
  116. </CollectionViewSource><CollectionViewSource x:Key="cvStudents" Source="{Binding Students}">
  117.         <CollectionViewSource.GroupDescriptions>
  118.                 <PropertyGroupDescription PropertyName="Class"/>
  119.                 <PropertyGroupDescription PropertyName="Sex"/>
  120.         </CollectionViewSource.GroupDescriptions>
  121. </CollectionViewSource>       
复制代码
 
DataGrid排序

在WPF中,实现DataGrid的排序,也是通过CollectionViewSource来实现。
1. 设置排序列

有两种方式可以设置DataGrid排序列,如下所示:
1.1 XAML中设置

通过设置CollectionViewSource的SortDescriptions属性,设置排序列和排序方向。如下所示:
  1. <CollectionViewSource x:Key="cvStudents" Source="{Binding Students}">
  2.         <CollectionViewSource.GroupDescriptions>
  3.                 <PropertyGroupDescription PropertyName="Class"/>
  4.                 <PropertyGroupDescription PropertyName="Sex"/>
  5.         </CollectionViewSource.GroupDescriptions>
  6. </CollectionViewSource>
复制代码
 其中scm是新定义的命名空间【xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"】
 
1.2 后台代码设置

在ViewModel中通过后台代码设置,同样也需要引入对应的命名空间,如下所示:
  1. ICollectionView cvTasks = CollectionViewSource.GetDefaultView(this.dataGrid.ItemsSource);
  2. if (cvTasks != null && cvTasks.CanSort == true)
  3. {
  4.         cvTasks.SortDescriptions.Clear();
  5.         cvTasks.SortDescriptions.Add(new SortDescription("No", ListSortDirection.Ascending));
  6.         cvTasks.SortDescriptions.Add(new SortDescription("Age", ListSortDirection.Ascending));
  7. }
复制代码
DataGrid整体示例

具备过滤,分组,排序的示例截图,如下所示:


参考文献

1. 官方文档:https://learn.microsoft.com/zh-cn/dotnet/desktop/wpf/controls/how-to-group-sort-and-filter-data-in-the-datagrid-control?view=netframeworkdesktop-4.8
 
以上就是【浅谈WPF之DataGrid过滤,分组,排序】的全部内容,希望能够一起学习,共同进步。

来源:https://www.cnblogs.com/hsiang/archive/2023/12/22/17920372.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具