如何:在 DataGrid 控件中对数据进行分组、排序和筛选

您所在的位置:网站首页 筛选后怎么显示标题栏内容 如何:在 DataGrid 控件中对数据进行分组、排序和筛选

如何:在 DataGrid 控件中对数据进行分组、排序和筛选

2024-07-14 01:51| 来源: 网络整理| 查看: 265

如何:在 DataGrid 控件中对数据进行分组、排序和筛选 项目05/04/2023

通过对数据进行分组、排序和筛选,以不同的方式查看 DataGrid 中的数据通常很有用。 要对 DataGrid 中的数据进行分组、排序和筛选,可以将其绑定到支持这些函数的 CollectionView。 然后,可以在不影响基础源数据的情况下处理 CollectionView 中的数据。 集合视图中的更改反映在 DataGrid 用户界面 (UI) 中。

CollectionView 类为实现 IEnumerable 接口的数据源提供分组和排序功能。 CollectionViewSource 类允许你从 XAML 设置 CollectionView 的属性。

在此示例中,Task 对象的集合绑定到一个 CollectionViewSource。 CollectionViewSource 用作 DataGrid 的 ItemsSource。 对 CollectionViewSource 执行分组、排序和筛选,并在 DataGrid UI 中显示。

DataGrid 中的分组数据

将 CollectionViewSource 用作 ItemsSource

要对 DataGrid 控件中的数据进行分组、排序和筛选,可以将 DataGrid 绑定到支持这些函数的 CollectionView。 在此示例中,DataGrid 绑定到 CollectionViewSource,后者为 Task 对象的 List 提供这些函数。

将 DataGrid 绑定到 CollectionViewSource

创建实现 IEnumerable 接口的数据集合。

如果使用 List 创建集合,则应创建继承自 List 的新类,而不是实例化 List 实例。 这使你能够在 XAML 中将数据绑定到集合。

注意

集合中的对象必须实现 INotifyPropertyChanged 已更改的接口和 IEditableObject 接口,以便 DataGrid 能够正确响应属性的更改和编辑。 有关详细信息,请参阅实现属性更改通知。

// Requires using System.Collections.ObjectModel; public class Tasks : ObservableCollection { // Creating the Tasks collection in this way enables data binding from XAML. } ' Requires Imports System.Collections.ObjectModel Public Class Tasks Inherits ObservableCollection(Of Task) ' Creating the Tasks collection in this way enables data binding from XAML. End Class

在 XAML 中,创建集合类的实例并设置 x:Key 指令。

在 XAML 中,创建 CollectionViewSource 类的实例并设置 x:Key 指令,然后将集合类的实例设置为 Source。

创建 DataGrid 类的一个实例,并将 ItemsSource 属性设置为 CollectionViewSource。

要从代码中访问 CollectionViewSource,请使用 GetDefaultView 方法获取对 CollectionViewSource 的引用。

ICollectionView cvTasks = CollectionViewSource.GetDefaultView(dataGrid1.ItemsSource); Dim cvTasks As ICollectionView = CollectionViewSource.GetDefaultView(dataGrid1.ItemsSource) 对 DataGrid 中的项进行分组

要指定如何对 DataGrid 中的项分组,请使用 PropertyGroupDescription 类型对源视图中的项进行分组。

使用 XAML 对 DataGrid 中的项进行分组

创建一个 PropertyGroupDescription,指定要按其分组的属性。 可以在 XAML 或代码中指定该属性。

在 XAML 中,将 PropertyName 设置要按其分组的属性的名称。

在代码中,将按其分组的属性的名称传递给构造函数。

将 PropertyGroupDescription 添加到 CollectionViewSource.GroupDescriptions 集合中。

将 PropertyGroupDescription 的其他实例添加到 GroupDescriptions 集合,以添加更多级别的分组。

ICollectionView cvTasks = CollectionViewSource.GetDefaultView(dataGrid1.ItemsSource); if (cvTasks != null && cvTasks.CanGroup == true) { cvTasks.GroupDescriptions.Clear(); cvTasks.GroupDescriptions.Add(new PropertyGroupDescription("ProjectName")); cvTasks.GroupDescriptions.Add(new PropertyGroupDescription("Complete")); } Dim cvTasks As ICollectionView = CollectionViewSource.GetDefaultView(dataGrid1.ItemsSource) If cvTasks IsNot Nothing And cvTasks.CanGroup = True Then cvTasks.GroupDescriptions.Clear() cvTasks.GroupDescriptions.Add(New PropertyGroupDescription("ProjectName")) cvTasks.GroupDescriptions.Add(New PropertyGroupDescription("Complete")) End If

要删除该组,请从 GroupDescriptions 集合中删除 PropertyGroupDescription。

要删除所有组,请调用 GroupDescriptions 集合的 Clear 方法。

ICollectionView cvTasks = CollectionViewSource.GetDefaultView(dataGrid1.ItemsSource); if (cvTasks != null) { cvTasks.GroupDescriptions.Clear(); } Dim cvTasks As ICollectionView = CollectionViewSource.GetDefaultView(dataGrid1.ItemsSource) If cvTasks IsNot Nothing Then cvTasks.GroupDescriptions.Clear() End If

在 DataGrid 中对项进行分组后,可以定义一个 GroupStyle 来指定每个组的外观。 通过将 GroupStyle 添加到 DataGrid 的 GroupStyle 集合来应用它。 如果你有多个级别的分组,则可以对每个级别的组应用不同的样式。 样式按照定义顺序应用。 例如,如果定义两种样式,则第一个样式将应用于顶级行组。 第二种样式将应用于第二级或更低级别的所有行组。 GroupStyle 的 DataContext 就是该组所代表的 CollectionViewGroup。

更改行组标题的外观

创建定义行组外观的 GroupStyle。

将 GroupStyle 放入 标记中。

对 DataGrid 中的项进行排序

要指定如何对 DataGrid 中的项进行排序,请使用 SortDescription 类型对源视图中的项进行排序。

对 DataGrid 中的项进行排序

创建一个 SortDescription,指定要按其排序的属性。 可以在 XAML 或代码中指定该属性。

在 XAML 中,将 PropertyName 设置要按其排序的属性的名称。

在代码中,将按其排序的属性的名称和 ListSortDirection 传递给构造函数。

将 SortDescription 添加到 CollectionViewSource.SortDescriptions 集合中。

将 SortDescription 的其他实例添加到 SortDescriptions 集合,以按其他属性排序。

// Requires using System.ComponentModel; ICollectionView cvTasks = CollectionViewSource.GetDefaultView(dataGrid1.ItemsSource); if (cvTasks != null && cvTasks.CanSort == true) { cvTasks.SortDescriptions.Clear(); cvTasks.SortDescriptions.Add(new SortDescription("ProjectName", ListSortDirection.Ascending)); cvTasks.SortDescriptions.Add(new SortDescription("Complete", ListSortDirection.Ascending)); cvTasks.SortDescriptions.Add(new SortDescription("DueDate", ListSortDirection.Ascending)); } Dim cvTasks As ICollectionView = CollectionViewSource.GetDefaultView(dataGrid1.ItemsSource) If cvTasks IsNot Nothing And cvTasks.CanSort = True Then cvTasks.SortDescriptions.Clear() cvTasks.SortDescriptions.Add(New SortDescription("ProjectName", ListSortDirection.Ascending)) cvTasks.SortDescriptions.Add(New SortDescription("Complete", ListSortDirection.Ascending)) cvTasks.SortDescriptions.Add(New SortDescription("DueDate", ListSortDirection.Ascending)) End If 对 DataGrid 中的项进行筛选

要使用 CollectionViewSource 筛选 DataGrid 中的项,需要在处理程序中为 CollectionViewSource.Filter 事件提供筛选逻辑。

筛选 DataGrid 中的项

为 CollectionViewSource.Filter 事件添加处理程序。

在 Filter 事件处理程序中,定义筛选逻辑。

每次刷新视图时,都会应用该筛选器。

private void CollectionViewSource_Filter(object sender, FilterEventArgs e) { Task t = e.Item as Task; if (t != null) // If filter is turned on, filter completed items. { if (this.cbCompleteFilter.IsChecked == true && t.Complete == true) e.Accepted = false; else e.Accepted = true; } } Private Sub CollectionViewSource_Filter(ByVal sender As System.Object, ByVal e As System.Windows.Data.FilterEventArgs) Dim t As Task = e.Item If t IsNot Nothing Then ' If filter is turned on, filter completed items. If Me.cbCompleteFilter.IsChecked = True And t.Complete = True Then e.Accepted = False Else e.Accepted = True End If End If End Sub

或者,可以创建一个提供筛选逻辑的方法并设置 CollectionView.Filter 属性来应用筛选器,从而筛选 DataGrid 中的项。 要查看此方法的示例,请参阅筛选视图中的数据。

示例

下面的示例演示如何对 CollectionViewSource 中的 Task 数据进行分组、排序和筛选,并在 DataGrid 中显示经过分组、排序和筛选的 Task 数据。 CollectionViewSource 用作 DataGrid 的 ItemsSource。 对 CollectionViewSource 执行分组、排序和筛选,并在 DataGrid UI 中显示。

要测试此示例,需要调整 DGGroupSortFilterExample 名称以匹配项目名称。 如果使用 Visual Basic,则需要将 Window 的类名更改为以下形式。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3