DevExpress WPF中文教程:Data Grid - 如何绑定到有限制的自定义服务(二)?

DevExpress WPF拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpress WPF能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案。 无论是Office办公软件的衍伸产品,还是以数据为中心的商业智能产品,都能通过DevExpress WPF控件来实现。

本文将演示如何将GridControl绑定到有限制的自定义服务(例如,不兼容的过滤器和排序),由于内容较多,我们将分为几篇文章来阐述,欢迎持续关注我们哟~

在上文中(点击这里回顾>>),我们为大家介绍了如何开始获取数据并启用滚动,本文将继续介绍如何启用排序,欢迎持续关注~

获取DevExpress WPF v25.1正式版下载

Step 2:启用排序

在将GridControl绑定到虚拟源之后,应该启用数据操作。

在这一步中,我们将描述如何启用排序功能:

  • 在虚拟源中实现数据排序操作。
  • 然后在GridControl中启用这些数据操作。

注意 :在本教程中,使用Issues Service作为数据源的示例。

概述

Issues Service允许您应用以下排序顺序:

  • Default - 记录从新的到旧的显示
  • Hot - 最近几天被关注最多的问题
  • Week - 本周浏览量最多的问题
  • Created Date(创建日期)
  • Votes(投票)

C#

cs 复制代码
public enum IssueSortOrder {
Default,
Hot,
Week,
CreatedAscending,
CreatedDescending,
VotesAscending,
VotesDescending,
}

在上文中,获取行默认排序顺序:

C#

cs 复制代码
public MainWindow() {
// ...
source.FetchRows += (o, e) => { e.Result = FetchRowsAsync(e); };
}
static async Task<FetchRowsResult> FetchRowsAsync(FetchRowsAsyncEventArgs e) {
IssueSortOrder sortOrder = GetIssueSortOrder(e);
const int pageSize = 30;
var issues = await IssuesService.GetIssuesAsync(page: e.Skip / pageSize, pageSize: pageSize, sortOrder: sortOrder, filter: null);
return new FetchRowsResult(issues, hasMoreRows: issues.Length == pageSize);
}
static IssueSortOrder GetIssueSortOrder(FetchRowsAsyncEventArgs e) {
return IssueSortOrder.Default;
}
启用排序
  1. 实现Hot和Week排序命令,注意GridControl没有这些对象的任何值。
  • 将How和Week列添加到GridControl中。
  • 通过将BaseColumn.Visible属性设置为false来隐藏GridControl视图中的这些列。
  • 通过将BaseColumn.ShowInColumnChooser属性设置为false,在列选择器中隐藏它们。
  • 为这些列添加自定义属性。

XAML

XML 复制代码
<dxg:GridControl x:Name="grid" >
<dxg:GridControl.Columns>
<!-- -->
<dxg:GridColumn FieldName="Hot" Visible="False" ShowInColumnChooser="False" />
<dxg:GridColumn FieldName="Week" Visible="False" ShowInColumnChooser="False" />
</dxg:GridControl.Columns>
</dxg:GridControl>

C#

cs 复制代码
static PropertyDescriptorCollection GetCustomProperties() {
var customProperties = TypeDescriptor.GetProperties(typeof(IssueData))
.Cast<PropertyDescriptor>()
.Where(x => x.Name != "Tags")
.Concat(new[] {
CreateTagsProperty(),
new DynamicPropertyDescriptor("Hot", typeof(string), x => null),
new DynamicPropertyDescriptor("Week", typeof(string), x => null)
})
.ToArray();
return new PropertyDescriptorCollection(customProperties);
}
  1. 在虚拟源中实现排序:

C#

cs 复制代码
static IssueSortOrder GetIssueSortOrder(FetchRowsAsyncEventArgs e) {
if(e.SortOrder.Length == 0)
return IssueSortOrder.Default;
var sort = e.SortOrder.Single();
switch(sort.PropertyName) {
case "Hot":
if(sort.Direction != ListSortDirection.Descending)
throw new InvalidOperationException();
return IssueSortOrder.Hot;
case "Week":
if(sort.Direction != ListSortDirection.Descending)
throw new InvalidOperationException();
return IssueSortOrder.Week;
case "Created":
return sort.Direction == ListSortDirection.Ascending
? IssueSortOrder.CreatedAscending
: IssueSortOrder.CreatedDescending;
case "Votes":
return sort.Direction == ListSortDirection.Ascending
? IssueSortOrder.VotesAscending
: IssueSortOrder.VotesDescending;
default:
return IssueSortOrder.Default;
}
}
  1. 允许在GridControl中排序:

XAML

XML 复制代码
<dxg:GridColumn FieldName="Created" AllowSorting="True" DefaultSortOrder="Descending" />
<dxg:GridColumn FieldName="Votes" AllowSorting="True" DefaultSortOrder="Descending" />
<dxg:GridColumn x:Name="hotColumn" FieldName="Hot" Visible="False" ShowInColumnChooser="False" AllowSorting="True" AllowedSortOrders="Descending" SortOrder="Descending" />
<dxg:GridColumn x:Name="weekColumn" FieldName="Week" Visible="False" ShowInColumnChooser="False" AllowSorting="True" AllowedSortOrders="Descending" />
  1. Hot和Week列在GridControl中是不可见的。

TableView.CompactPanelShowModeTableView.CompactSortElementShowMode属性设置为Always,来允许最终用户指定紧凑面板中的排序顺序:

XAML

XML 复制代码
<dxg:TableView CompactPanelShowMode="Always" CompactSortElementShowMode="Always" />

下图显示了结果:

相关推荐
晚风一隅1 天前
阿里云盘古存储系统:EB级分布式存储的架构革命与技术突破
wpf
步步为营DotNet1 天前
深挖.NET 11:.NET Aspire 在云原生应用状态管理的创新与实践
云原生·.net·wpf
He BianGu2 天前
【项目】WPF VisionMaster 4.0 项目介绍和开发文档
c#·wpf·流程图·开发文档·机器视觉·visionmaster
He BianGu2 天前
【笔记】在WPF中PriorityBinding的详细介绍
笔记·wpf
bugcome_com2 天前
WPF + Prism 技术指南与实战项目(一、自行搭建)
wpf·prism
她说彩礼65万3 天前
WPF Border
wpf
He BianGu3 天前
【笔记】在WPF中在IValueConverter 时“无法返回有效值该怎么做”
笔记·wpf
海兰4 天前
使用 OpenTelemetry 与 Elastic APM 追踪 MCP 服务器工具调用
运维·服务器·elasticsearch·wpf
咩图4 天前
WPF-VisualStudio-C#-Fluent.Ribbon8.0.0学习
c#·wpf·visual studio
拼尽全力前进4 天前
JDDL 核心实现原理与架构解析
架构·wpf