让WPF中的DataGrid像Excel一样可以筛选(上)

简介: 让WPF中的DataGrid像Excel一样可以筛选(上)

在默认情况下,WPF提供的DataGrid仅拥有数据展示等简单功能,如果要实现像Excel一样复杂的筛选过滤功能,则相对比较麻烦。本文以一个简单的小例子,简述如何通过WPF实话DataGrid的筛选功能,仅供学习分享使用,如有不足之处,还请指正。

涉及知识点


在本示例中,从数据绑定,到数据展示,涉及知识点如下所示:

  • DataGrid,要WPF提供的进行二维数据展示在列表控件,默认功能非常简单,但是可以通过数据模板或者控件模板进行扩展和美化,可伸缩性很强。
  • MVVM,是Model-View-ViewModel的简写,主要进行数据和UI进行前后端分离,在本示例中,主要用到的MVVM第三方库为CommunityToolkit.Mvvm,大大简化原生MVVM的实现方式。
  • 集合视图, 要对 DataGrid 中的数据进行分组、排序和筛选,可以将其绑定到支持这些函数的 CollectionView。 然后,可以在不影响基础源数据的情况下处理 CollectionView 中的数据。 集合视图中的更改反映在 DataGrid 用户界面 (UI) 中。
  • Popup控件,直接继承FrameworkElement,提供了一种在单独的窗口中显示内容的方法,该窗口相对于指定的元素或屏幕坐标,浮动在当前Popup应用程序窗口上,可用于悬浮窗口。

示例截图


本示例主要模仿Excel的筛选功能进行实现,右键标题栏打开浮动窗口,悬浮于标题栏下方,既可以通过文本框进行筛选,也可以通过筛选按钮弹出右键菜单,选择具体筛选方式,截图如下所示:

选择筛选方式,弹出窗口,如下所示:

输入筛选条件,点击确定,或者取消筛选。如筛选学号里面包含2的,效果如下所示:

注意:以上筛选都是客户端筛选,不会修改数据源,也不会重连数据库。

核心源码


在本示例中,核心源码主要包含以下几个部分:

1. 前端视图【MainWindow.xaml】源码

主要实现了按学号,姓名,年龄三列进行筛选,既可以单列筛选,又可以组合筛选。且三列的筛选实现方式一致,仅是绑定列有差异。

1. <Window x:Class="DemoDataGrid.MainWindow"
2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6. xmlns:local="clr-namespace:DemoDataGrid"
7. mc:Ignorable="d"
8. Title="DataGrid筛选示例" Height="650" Width="800">
9. <Window.Resources>
10. <ResourceDictionary>
11. <CollectionViewSource x:Key="ItemsSource" Source="{Binding Path=Students}"></CollectionViewSource>
12. <CollectionViewSource x:Key="Names" Source="{Binding Path=Names}"></CollectionViewSource>
13. <CollectionViewSource x:Key="Nos" Source="{Binding Path=Nos}"></CollectionViewSource>
14. <CollectionViewSource x:Key="Ages" Source="{Binding Path=Ages}"></CollectionViewSource>
15. <Style x:Key="ListBoxStyle" TargetType="{x:Type ListBox}">
16. <Setter Property="ScrollViewer.CanContentScroll" Value="True"></Setter>
17. <Setter Property="Template">
18. <Setter.Value>
19. <ControlTemplate TargetType="{x:Type ListBox}">
20. <ScrollViewer x:Name="ScrollViewer" CanContentScroll="True">
21. <ItemsPresenter></ItemsPresenter>
22. </ScrollViewer>
23. </ControlTemplate>
24. </Setter.Value>
25. </Setter>
26. </Style>
27. <Geometry x:Key="Icon_Filter">
28.                 M608 864C588.8 864 576 851.2 576 832L576 448c0-6.4 6.4-19.2 12.8-25.6L787.2 256c6.4-6.4 6.4-19.2 0-19.2 0-6.4-6.4-12.8-19.2-12.8L256 224c-12.8 0-19.2 6.4-19.2 12.8 0 6.4-6.4 12.8 6.4 19.2l198.4 166.4C441.6 428.8 448 441.6 448 448l0 256c0 19.2-12.8 32-32 32S384 723.2 384 704L384 460.8 198.4 307.2c-25.6-25.6-32-64-19.2-96C185.6 179.2 217.6 160 256 160L768 160c32 0 64 19.2 76.8 51.2 12.8 32 6.4 70.4-19.2 89.6l-192 160L633.6 832C640 851.2 627.2 864 608 864z
29. </Geometry>
30. <ContextMenu x:Key="queryConditionMenu" MouseLeave="ContextMenu_MouseLeave" MenuItem.Click="ContextMenu_Click">
31. <MenuItem FontSize="12" Header="等于" Tag="Equal"></MenuItem>
32. <MenuItem FontSize="12" Header="不等于"  Tag="NotEqual"></MenuItem>
33. <MenuItem FontSize="12" Header="开头"  Tag="Begin"></MenuItem>
34. <MenuItem FontSize="12" Header="结尾"  Tag="End"></MenuItem>
35. <MenuItem FontSize="12" Header="包含"  Tag="In"></MenuItem>
36. <MenuItem FontSize="12" Header="不包含"  Tag="NotIn"></MenuItem>
37. </ContextMenu>
38. </ResourceDictionary>
39. 
40. </Window.Resources>
41. <Grid Margin="10">
42. <Grid.RowDefinitions>
43. <RowDefinition Height="20"></RowDefinition>
44. <RowDefinition Height="*"></RowDefinition>
45. </Grid.RowDefinitions>
46. <DataGrid Grid.Row="1" x:Name="dgStudents" ItemsSource="{Binding Source={StaticResource ItemsSource} }" AutoGenerateColumns="False"
47. CanUserReorderColumns="True" CanUserDeleteRows="False" CanUserAddRows="False" HeadersVisibility="Column"
48. CanUserSortColumns="True"
49. VirtualizingPanel.VirtualizationMode="Recycling"
50. EnableColumnVirtualization="True" VirtualizingPanel.IsVirtualizingWhenGrouping="True" GridLinesVisibility="All" RowHeight="25"
51. SelectionUnit="FullRow" SelectionMode="Single" IsReadOnly="True" FontSize="12"
52. SelectedIndex="{Binding SelectTaskItemIndex}" SelectedItem="{Binding SelectTaskItem}"
53. CanUserResizeColumns="True">
54. <DataGrid.Columns>
55. <DataGridTextColumn Binding="{Binding Id}" Header="Id" Width="*">
56. 
57. </DataGridTextColumn>
58. <DataGridTextColumn Binding="{Binding No}" Width="*">
59. <DataGridTextColumn.Header>
60. <TextBlock Text="学号" FontWeight="Regular" MouseRightButtonDown="TextBlock_MouseRightButtonDown" Tag="No"></TextBlock>
61. </DataGridTextColumn.Header>
62. </DataGridTextColumn>
63. <DataGridTextColumn Binding="{Binding Name}" Width="*">
64. <DataGridTextColumn.Header>
65. <TextBlock Text="姓名" FontWeight="Regular" MouseRightButtonDown="TextBlock_MouseRightButtonDown" Tag="Name"></TextBlock>
66. </DataGridTextColumn.Header>
67. </DataGridTextColumn>
68. <DataGridTextColumn Binding="{Binding Age}" Width="*">
69. <DataGridTextColumn.Header>
70. <TextBlock Text="年龄" FontWeight="Regular" MouseRightButtonDown="TextBlock_MouseRightButtonDown" Tag="Age"></TextBlock>
71. </DataGridTextColumn.Header>
72. </DataGridTextColumn>
73. </DataGrid.Columns>
74. </DataGrid>
75. <Popup x:Name="popupNo" Width="135" MaxHeight="500" Height="Auto" PopupAnimation="Slide" AllowsTransparency="False" MouseLeave="popup_MouseLeave">
76. <Border BorderBrush="LightBlue" BorderThickness="1" Padding="5" Background="AliceBlue">
77. <Grid>
78. <Grid.ColumnDefinitions>
79. <ColumnDefinition></ColumnDefinition>
80. <ColumnDefinition></ColumnDefinition>
81. </Grid.ColumnDefinitions>
82. <Grid.RowDefinitions>
83. <RowDefinition></RowDefinition>
84. <RowDefinition></RowDefinition>
85. <RowDefinition></RowDefinition>
86. </Grid.RowDefinitions>
87. <StackPanel Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Orientation="Horizontal">
88. <TextBox Height="25" x:Name="txtNo" MinWidth="60" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Tag="No" TextChanged="TextBox_TextChanged"></TextBox>
89. <Button x:Name="btnNoFilter"  Tag="No" ClickMode="Press" Click="ButtonFilter_Click" ContextMenu="{StaticResource queryConditionMenu}">
90. <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
91. <Path Data="{StaticResource Icon_Filter}" Stroke="Gray" StrokeThickness="1"
92. Height="12" Width="12" Stretch="Fill"></Path>
93. <TextBlock Margin="2,0" Text="筛选" FontSize="12"></TextBlock>
94. </StackPanel>
95. </Button>
96. </StackPanel>
97. <ListBox x:Name="lbNos" ItemsSource="{Binding Source={StaticResource Nos}}" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" VirtualizingPanel.VirtualizationMode="Recycling" VirtualizingPanel.IsVirtualizing="True" Style="{StaticResource ListBoxStyle}">
98. <ListBox.ItemTemplate>
99. <DataTemplate>
100. <CheckBox Content="{Binding FilterText}" IsChecked="{Binding IsChecked}"></CheckBox>
101. </DataTemplate>
102. </ListBox.ItemTemplate>
103. </ListBox>
104. 
105. <Button Tag="No" Content="取消" Width="60" HorizontalAlignment="Left" Grid.Row="2" Grid.Column="0" Click="btnCancel_Click"></Button>
106. <Button Content="确定" Width="60" HorizontalAlignment="Right" Grid.Row="2" Grid.Column="1" Click="btnOk_Click"></Button>
107. </Grid>
108. </Border>
109. </Popup>
110. <Popup x:Name="popupName" Width="135" MaxHeight="500" Height="Auto" PopupAnimation="Slide" AllowsTransparency="False" MouseLeave="popup_MouseLeave">
111. <Border BorderBrush="LightBlue" BorderThickness="1" Padding="5" Background="AliceBlue">
112. <Grid>
113. <Grid.ColumnDefinitions>
114. <ColumnDefinition></ColumnDefinition>
115. <ColumnDefinition></ColumnDefinition>
116. </Grid.ColumnDefinitions>
117. <Grid.RowDefinitions>
118. <RowDefinition></RowDefinition>
119. <RowDefinition></RowDefinition>
120. <RowDefinition></RowDefinition>
121. </Grid.RowDefinitions>
122. <StackPanel Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Orientation="Horizontal">
123. <TextBox Height="25" x:Name="txtName" MinWidth="60" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Tag="Name" TextChanged="TextBox_TextChanged"></TextBox>
124. <Button x:Name="btnNameFilter"  Tag="Name" Click="ButtonFilter_Click" ContextMenu="{StaticResource queryConditionMenu}">
125. <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
126. <Path Data="{StaticResource Icon_Filter}" Stroke="Gray" StrokeThickness="1"
127. Height="12" Width="12" Stretch="Fill"></Path>
128. <TextBlock Margin="2,0" Text="筛选" FontSize="12"></TextBlock>
129. </StackPanel>
130. </Button>
131. </StackPanel>
132. <ListBox x:Name="lbNames" ItemsSource="{Binding Source={StaticResource Names}}" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" VirtualizingPanel.VirtualizationMode="Recycling" VirtualizingPanel.IsVirtualizing="True" Style="{StaticResource ListBoxStyle}">
133. <ListBox.ItemTemplate>
134. <DataTemplate>
135. <CheckBox Content="{Binding FilterText}" IsChecked="{Binding IsChecked}"></CheckBox>
136. </DataTemplate>
137. </ListBox.ItemTemplate>
138. </ListBox>
139. 
140. <Button Tag="No" Content="取消" Width="60" HorizontalAlignment="Left" Grid.Row="2" Grid.Column="0" Click="btnCancel_Click"></Button>
141. <Button Content="确定" Width="60" HorizontalAlignment="Right" Grid.Row="2" Grid.Column="1" Click="btnOk_Click"></Button>
142. </Grid>
143. </Border>
144. </Popup>
145. <Popup x:Name="popupAge" Width="135" MaxHeight="500" Height="Auto" PopupAnimation="Slide" AllowsTransparency="False" MouseLeave="popup_MouseLeave">
146. <Border BorderBrush="LightBlue" BorderThickness="1" Padding="5" Background="AliceBlue">
147. <Grid>
148. <Grid.ColumnDefinitions>
149. <ColumnDefinition></ColumnDefinition>
150. <ColumnDefinition></ColumnDefinition>
151. </Grid.ColumnDefinitions>
152. <Grid.RowDefinitions>
153. <RowDefinition></RowDefinition>
154. <RowDefinition></RowDefinition>
155. <RowDefinition></RowDefinition>
156. </Grid.RowDefinitions>
157. <StackPanel Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Orientation="Horizontal">
158. <TextBox Height="25" x:Name="txtAge" MinWidth="60" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Tag="Age" TextChanged="TextBox_TextChanged"></TextBox>
159. <Button x:Name="btnAgeFilter"  Tag="Age" Click="ButtonFilter_Click" ContextMenu="{StaticResource queryConditionMenu}">
160. <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
161. <Path Data="{StaticResource Icon_Filter}" Stroke="Gray" StrokeThickness="1"
162. Height="12" Width="12" Stretch="Fill"></Path>
163. <TextBlock Margin="2,0" Text="筛选" FontSize="12"></TextBlock>
164. </StackPanel>
165. </Button>
166. </StackPanel>
167. <ListBox x:Name="lbAges" ItemsSource="{Binding Source={StaticResource Ages}}" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" VirtualizingPanel.VirtualizationMode="Recycling" VirtualizingPanel.IsVirtualizing="True" Style="{StaticResource ListBoxStyle}">
168. <ListBox.ItemTemplate>
169. <DataTemplate>
170. <CheckBox Content="{Binding FilterText}" IsChecked="{Binding IsChecked}"></CheckBox>
171. </DataTemplate>
172. </ListBox.ItemTemplate>
173. </ListBox>
174. 
175. <Button Tag="No" Content="取消" Width="60" HorizontalAlignment="Left" Grid.Row="2" Grid.Column="0" Click="btnCancel_Click"></Button>
176. <Button Content="确定" Width="60" HorizontalAlignment="Right" Grid.Row="2" Grid.Column="1" Click="btnOk_Click"></Button>
177. </Grid>
178. </Border>
179. </Popup>
180. 
181. <Popup x:Name="popupNoMenu" Width="300" MaxHeight="500" Height="200" PopupAnimation="Slide" AllowsTransparency="False" Tag="">
182. <Border BorderThickness="1" BorderBrush="Beige" Padding="15" Background="AliceBlue">
183. <Grid>
184. <Grid.ColumnDefinitions>
185. <ColumnDefinition></ColumnDefinition>
186. <ColumnDefinition></ColumnDefinition>
187. </Grid.ColumnDefinitions>
188. <Grid.RowDefinitions>
189. <RowDefinition Height="Auto"></RowDefinition>
190. <RowDefinition Height="Auto"></RowDefinition>
191. <RowDefinition Height="Auto"></RowDefinition>
192. <RowDefinition></RowDefinition>
193. </Grid.RowDefinitions>
194. <StackPanel Orientation="Horizontal" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2">
195. <TextBlock Text="学号" VerticalAlignment="Center"></TextBlock>
196. <ComboBox x:Name="combNoMenu1" Height="28" Width="100" Margin="4" VerticalContentAlignment="Center">
197. <ComboBoxItem Content="等于" ></ComboBoxItem>
198. <ComboBoxItem Content="不等于"></ComboBoxItem>
199. <ComboBoxItem Content="开头"></ComboBoxItem>
200. <ComboBoxItem Content="结尾"></ComboBoxItem>
201. <ComboBoxItem Content="包含"></ComboBoxItem>
202. <ComboBoxItem Content="不包含"></ComboBoxItem>
203. </ComboBox>
204. <TextBox x:Name="txtNoMenu1" Height="28" Width="100" VerticalContentAlignment="Center"></TextBox>
205. </StackPanel>
206. <StackPanel Orientation="Horizontal" Grid.Row="1" Grid.Column="0"  Grid.ColumnSpan="2">
207. <RadioButton x:Name="rbNoAnd" Content="与" IsChecked="True" Margin="4" Background="AliceBlue" VerticalAlignment="Center" VerticalContentAlignment="Center"></RadioButton>
208. <RadioButton x:Name="rbNoOr" Content="或" Background="AliceBlue" VerticalAlignment="Center" VerticalContentAlignment="Center"></RadioButton>
209. </StackPanel>
210. <StackPanel Orientation="Horizontal" Grid.Row="2" Grid.Column="0"  Grid.ColumnSpan="2">
211. <TextBlock Text="学号" VerticalAlignment="Center"></TextBlock>
212. <ComboBox x:Name="combNoMenu2" Height="28" Margin="4" Width="100" VerticalContentAlignment="Center">
213. <ComboBoxItem Content="等于" ></ComboBoxItem>
214. <ComboBoxItem Content="不等于"></ComboBoxItem>
215. <ComboBoxItem Content="开头"></ComboBoxItem>
216. <ComboBoxItem Content="结尾"></ComboBoxItem>
217. <ComboBoxItem Content="包含"></ComboBoxItem>
218. <ComboBoxItem Content="不包含"></ComboBoxItem>
219. </ComboBox>
220. <TextBox x:Name="txtNoMenu2" Height="28" Width="100" VerticalContentAlignment="Center"></TextBox>
221. </StackPanel>
222. <Button Tag="No" Content="取消" Width="100" Height="28" HorizontalAlignment="Left" Grid.Row="3" Grid.Column="0" Click="btnCancelFilter_Click"></Button>
223. <Button Tag="No" Content="确定" Width="100" Height="28" HorizontalAlignment="Right" Grid.Row="3" Grid.Column="1" Click="btnOkFilter_Click"></Button>
224. </Grid>
225. </Border>
226. </Popup>
227. <Popup x:Name="popupNameMenu" Width="300" MaxHeight="500" Height="200" PopupAnimation="Slide" AllowsTransparency="False" Tag="">
228. <Border BorderThickness="1" BorderBrush="Beige" Padding="15" Background="AliceBlue">
229. <Grid>
230. <Grid.RowDefinitions>
231. <RowDefinition Height="Auto"></RowDefinition>
232. <RowDefinition Height="Auto"></RowDefinition>
233. <RowDefinition Height="Auto"></RowDefinition>
234. <RowDefinition Height="Auto"></RowDefinition>
235. </Grid.RowDefinitions>
236. <StackPanel Orientation="Horizontal" Grid.Row="0" Grid.Column="0">
237. <TextBlock Text="姓名" VerticalAlignment="Center"></TextBlock>
238. <ComboBox x:Name="combNameMenu1" Height="28" Width="100" Margin="4" VerticalContentAlignment="Center">
239. <ComboBoxItem Content="等于" ></ComboBoxItem>
240. <ComboBoxItem Content="不等于"></ComboBoxItem>
241. <ComboBoxItem Content="开头"></ComboBoxItem>
242. <ComboBoxItem Content="结尾"></ComboBoxItem>
243. <ComboBoxItem Content="包含"></ComboBoxItem>
244. <ComboBoxItem Content="不包含"></ComboBoxItem>
245. </ComboBox>
246. <TextBox x:Name="txtNameMenu1" Height="28" Width="100" VerticalContentAlignment="Center"></TextBox>
247. </StackPanel>
248. <StackPanel Orientation="Horizontal" Grid.Row="1" Grid.Column="0">
249. <RadioButton x:Name="rbNameAnd" Content="与" IsChecked="True" Margin="4" Background="AliceBlue" VerticalAlignment="Center" VerticalContentAlignment="Center"></RadioButton>
250. <RadioButton x:Name="rbNameOr" Content="或" Background="AliceBlue" VerticalAlignment="Center" VerticalContentAlignment="Center"></RadioButton>
251. </StackPanel>
252. <StackPanel Orientation="Horizontal" Grid.Row="2" Grid.Column="0">
253. <TextBlock Text="姓名" VerticalAlignment="Center"></TextBlock>
254. <ComboBox x:Name="combNameMenu2" Height="28" Width="100" Margin="4" VerticalContentAlignment="Center">
255. <ComboBoxItem Content="等于" ></ComboBoxItem>
256. <ComboBoxItem Content="不等于"></ComboBoxItem>
257. <ComboBoxItem Content="开头"></ComboBoxItem>
258. <ComboBoxItem Content="结尾"></ComboBoxItem>
259. <ComboBoxItem Content="包含"></ComboBoxItem>
260. <ComboBoxItem Content="不包含"></ComboBoxItem>
261. </ComboBox>
262. <TextBox x:Name="txtNameMenu2" Height="28" Width="100" VerticalContentAlignment="Center"></TextBox>
263. </StackPanel>
264. <Button Tag="Name" Content="取消" Width="100" Height="28" HorizontalAlignment="Left" Grid.Row="3" Grid.Column="0" Click="btnCancelFilter_Click"></Button>
265. <Button Tag="Name" Content="确定" Width="100" Height="28" HorizontalAlignment="Right" Grid.Row="3" Grid.Column="1" Click="btnOkFilter_Click"></Button>
266. </Grid>
267. </Border>
268. </Popup>
269. <Popup x:Name="popupAgeMenu" Width="300" MaxHeight="500" Height="200" PopupAnimation="Slide" AllowsTransparency="False" Tag="">
270. <Border BorderThickness="1" BorderBrush="Beige" Padding="15" Background="AliceBlue">
271. <Grid>
272. <Grid.RowDefinitions>
273. <RowDefinition Height="Auto"></RowDefinition>
274. <RowDefinition Height="Auto"></RowDefinition>
275. <RowDefinition Height="Auto"></RowDefinition>
276. <RowDefinition Height="Auto"></RowDefinition>
277. </Grid.RowDefinitions>
278. <StackPanel Orientation="Horizontal" Grid.Row="0" Grid.Column="0">
279. <TextBlock Text="年龄" VerticalAlignment="Center"></TextBlock>
280. <ComboBox x:Name="combAgeMenu1" Height="28" Width="100" Margin="4" VerticalContentAlignment="Center">
281. <ComboBoxItem Content="等于" ></ComboBoxItem>
282. <ComboBoxItem Content="不等于"></ComboBoxItem>
283. <ComboBoxItem Content="开头"></ComboBoxItem>
284. <ComboBoxItem Content="结尾"></ComboBoxItem>
285. <ComboBoxItem Content="包含"></ComboBoxItem>
286. <ComboBoxItem Content="不包含"></ComboBoxItem>
287. </ComboBox>
288. <TextBox x:Name="txtAgeMenu1" Height="28" Width="100" VerticalContentAlignment="Center"></TextBox>
289. </StackPanel>
290. <StackPanel Orientation="Horizontal" Grid.Row="1" Grid.Column="0">
291. <RadioButton x:Name="rbAgeAnd" Content="与"  IsChecked="True" Margin="4" Background="AliceBlue" VerticalAlignment="Center" VerticalContentAlignment="Center"></RadioButton>
292. <RadioButton x:Name="rbAgeOr" Content="或" Background="AliceBlue" VerticalAlignment="Center" VerticalContentAlignment="Center"></RadioButton>
293. </StackPanel>
294. <StackPanel Orientation="Horizontal" Grid.Row="2" Grid.Column="0">
295. <TextBlock Text="年龄" VerticalAlignment="Center"></TextBlock>
296. <ComboBox x:Name="combAgeMenu2" Height="28" Width="100" Margin="4" VerticalContentAlignment="Center">
297. <ComboBoxItem Content="等于" ></ComboBoxItem>
298. <ComboBoxItem Content="不等于"></ComboBoxItem>
299. <ComboBoxItem Content="开头"></ComboBoxItem>
300. <ComboBoxItem Content="结尾"></ComboBoxItem>
301. <ComboBoxItem Content="包含"></ComboBoxItem>
302. <ComboBoxItem Content="不包含"></ComboBoxItem>
303. </ComboBox>
304. <TextBox x:Name="txtAgeMenu2" Height="28" Width="100" VerticalContentAlignment="Center"></TextBox>
305. </StackPanel>
306. <Button Tag="Age" Content="取消" Width="100" Height="28" HorizontalAlignment="Left" Grid.Row="3" Grid.Column="0" Click="btnCancelFilter_Click"></Button>
307. <Button Tag="Age" Content="确定" Width="100" Height="28" HorizontalAlignment="Right" Grid.Row="3" Grid.Column="1" Click="btnOkFilter_Click"></Button>
308. </Grid>
309. </Border>
310. </Popup>
311. </Grid>
312. </Window>

2. 业务逻辑【MainWindowViewModel】

业务逻辑处理主要复责数据初始化等业务相关内容,和UI无关,如下所示:

1. using CommunityToolkit.Mvvm.ComponentModel;
2. using System;
3. using System.Collections.Generic;
4. using System.Linq;
5. using System.Text;
6. using System.Threading.Tasks;
7. 
8. namespace DemoDataGrid
9. {
10. public class MainWindowViewModel:ObservableObject
11.     {
12. #region 属性及构造函数
13. 
14. private List<Student> students;
15. 
16. public List<Student> Students
17.         {
18. get { return students; }
19. set { SetProperty(ref students, value); }
20.         }
21. 
22. private List<FilterInfo> names;
23. 
24. public List<FilterInfo> Names
25.         {
26. get { return names; }
27. set { SetProperty(ref names, value); }
28.         }
29. 
30. private List<FilterInfo> nos;
31. 
32. public List<FilterInfo> Nos
33.         {
34. get { return nos; }
35. set {SetProperty(ref nos , value); }
36.         }
37. 
38. private List<FilterInfo> ages;
39. 
40. public List<FilterInfo> Ages
41.         {
42. get { return ages; }
43. set {SetProperty(ref ages , value); }
44.         }
45. 
46. 
47. 
48. public MainWindowViewModel()
49.         {
50. this.Students= new List<Student>();
51. for (int i = 0; i < 20; i++) {
52. this.Students.Add(new Student()
53.                 {
54.                     Id = i,
55.                     Name = $"张{i}牛",
56.                     Age = (i % 10) + 10,
57.                     No = i.ToString().PadLeft(4, '0'),
58.                 });
59.             }
60. this.Nos= new List<FilterInfo>();
61. this.Names= new List<FilterInfo>();
62. this.Ages= new List<FilterInfo>();
63. this.Students.ForEach(s => {
64. this.Nos.Add(new FilterInfo() { FilterText=s.No,IsChecked=false });
65. this.Names.Add(new FilterInfo() { FilterText = s.Name, IsChecked = false });
66. this.Ages.Add(new FilterInfo() { FilterText = s.Age.ToString(), IsChecked = false });
67.             });
68. this.Ages=this.Ages.Distinct().ToList();//去重
69.         }
70. 
71. #endregion
72. 
73. 
74.     }
75. }


相关文章
|
10天前
|
数据采集 数据可视化 数据处理
【办公自动化】在Excel中按条件筛选数据并存入新的表2.0
【办公自动化】在Excel中按条件筛选数据并存入新的表2.0
56 1
|
10天前
|
前端开发 C#
浅谈WPF之DataGrid动态生成列
在日常开发中,DataGrid作为二维表格,非常适合数据的展示和统计。通常情况下,一般都有固定的格式和确定的数据列展示,但是在某些特殊情况下,也可能会需要用到动态生成列。本文以一些简单的小例子,简述在WPF开发中,如何动态生成DataGrid的行和列,仅供学习分享使用,如有不足之处,还请指正。
126 2
|
10天前
excel将筛选项的行全删掉
excel将筛选项的行全删掉
|
10天前
|
数据采集 数据可视化 数据处理
【办公自动化】在Excel中按条件筛选数据并存入新的表
【办公自动化】在Excel中按条件筛选数据并存入新的表
42 0
|
9月前
|
数据挖掘 数据处理 C#
WPF技术之DataGrid控件
WPF DataGrid是一种可以显示和编辑数据的界面控件。它可以作为表格形式展示数据,支持添加、删除、修改、排序和分组操作。
200 0
|
10天前
|
存储 数据可视化 定位技术
Python用若干列的数据多条件筛选、去除Excel数据并批量绘制直方图
Python用若干列的数据多条件筛选、去除Excel数据并批量绘制直方图
|
10天前
|
存储 定位技术 数据处理
Python基于指定范围筛选并剔除Excel表格中的数据
Python基于指定范围筛选并剔除Excel表格中的数据
|
10月前
|
C#
让WPF中的DataGrid像Excel一样可以筛选(下)
让WPF中的DataGrid像Excel一样可以筛选(下)
132 0
|
10天前
|
C# 开发者 Windows
基于Material Design风格开源、易用、强大的WPF UI控件库
基于Material Design风格开源、易用、强大的WPF UI控件库
|
10天前
|
C#
浅谈WPF之装饰器实现控件锚点
使用过visio的都知道,在绘制流程图时,当选择或鼠标移动到控件时,都会在控件的四周出现锚点,以便于修改大小,移动位置,或连接线等,那此功能是如何实现的呢?在WPF开发中,想要在控件四周实现锚点,可以通过装饰器来实现,今天通过一个简单的小例子,简述如何在WPF开发中,应用装饰器,仅供学习分享使用,如有不足之处,还请指正。
72 1