DevExpress WPF拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpress WPF能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案。 无论是Office办公软件的衍伸产品,还是以数据为中心的商业智能产品,都能通过DevExpress WPF控件来实现。
本文将演示如何将GridControl绑定到有限制的自定义服务(例如,不兼容的过滤器和排序),由于内容较多,我们将分为几篇文章来阐述,欢迎持续关注我们哟~
在上文中(点击这里回顾>>),我们为大家介绍了如何启用排序,本文将继续介绍如何启用过滤功能,,欢迎持续关注~
DevExpress技术交流群12:1028386091 欢迎一起进群讨论
Step 3:启用过滤
在将GridControl绑定到虚拟源之后,应该启用数据操作。
在这一步中,我们将描述如何启用过滤功能:
- 在虚拟源中实现过滤数据操作。
- 在GridControl中启用这些数据操作。

注意:在本教程中,使用Issues Service作为数据源的示例。
概述
Issues Service可以获取行:
- 在一段时间内(在CreatedFrom和CreatedTo之间)。
- 票数最多或最少的。
- 使用指定的标签。
C#
public class IssueFilter {
public DateTime? CreatedFrom { get; private set; }
public DateTime? CreatedTo { get; private set; }
public int? MinVotes { get; private set; }
public int? MaxVotes { get; private set; }
public string Tag { get; private set; }
}
在步骤1:获取数据和启用滚动,你没有过滤获取行:
C#
public MainWindow() {
// ...
source.FetchRows += (o, e) => {
e.Result = FetchRowsAsync(e);
};
}
static async Task<FetchRowsResult> FetchRowsAsync(FetchRowsAsyncEventArgs e) {
IssueFilter filter = MakeIssueFilter(e.Filter);
const int pageSize = 30;
var issues = await IssuesService.GetIssuesAsync(
page: e.Skip / pageSize,
pageSize: pageSize,
sortOrder: IssueSortOrder.Default,
filter: filter);
return new FetchRowsResult(issues, hasMoreRows: issues.Length == pageSize);
}
static IssueFilter MakeIssueFilter(CriteriaOperator filter) {
return null;
}
启用过滤
1. 在虚拟源中实现过滤:
- 使用FetchEventArgsBase.Filter属性获取GridControl的过滤。
- 解析过滤并返回一个过滤器, InfiniteAsyncSource.FetchRows事件处理程序在获取行时会考虑这个过滤器。
C#
static IssueFilter MakeIssueFilter(CriteriaOperator filter) {
return filter.Match(
binary: (propertyName, value, type) => {
switch(propertyName) {
case "Votes":
if(type == BinaryOperatorType.GreaterOrEqual)
return new IssueFilter(minVotes: (int)value);
if(type == BinaryOperatorType.LessOrEqual)
return new IssueFilter(maxVotes: (int)value);
throw new InvalidOperationException();
case "Created":
if(type == BinaryOperatorType.GreaterOrEqual)
return new IssueFilter(createdFrom: (DateTime)value);
if(type == BinaryOperatorType.Less)
return new IssueFilter(createdTo: (DateTime)value);
throw new InvalidOperationException();
case "Tags":
if(type == BinaryOperatorType.Equal)
return new IssueFilter(tag: (string)value);
throw new InvalidOperationException();
default:
throw new InvalidOperationException();
}
},
and: filters => {
return new IssueFilter(
createdFrom: filters.Select(x => x.CreatedFrom).SingleOrDefault(x => x != null),
createdTo: filters.Select(x => x.CreatedTo).SingleOrDefault(x => x != null),
minVotes: filters.Select(x => x.MinVotes).SingleOrDefault(x => x != null),
maxVotes: filters.Select(x => x.MaxVotes).SingleOrDefault(x => x != null),
tag: filters.Select(x => x.Tag).SingleOrDefault(x => x != null)
);
},
@null: default(IssueFilter)
);
}
2. 获取标签列表,并在tags列的下拉过滤器中显示它们:

- 处理InfiniteAsyncSource.GetUniqueValues事件。
- 使用Issues Service的GetTagsAsync方法获取标记列表。
- 指定GetUniqueValuesAsyncEventArgs.Result属性。
C#
source.GetUniqueValues += (o, e) => {
e.Result = GetUniqueValuesAsync(e.PropertyName);
};
static async Task<object[]> GetUniqueValuesAsync(string propertyName) {
if(propertyName == "Tags")
return await IssuesService.GetTagsAsync();
throw new InvalidOperationException();
}
3. 允许在GridControl中过滤:
XAML
<dxg:GridColumn FieldName="Created" AllowedDateTimeFilters="SingleDateRange" FilterPopupMode="DateSmart" /> <dxg:GridColumn FieldName="Votes" AllowedBinaryFilters="GreaterOrEqual,LessOrEqual" FilterPopupMode="Excel" /> <dxg:GridColumn FieldName="Tags" AllowedBinaryFilters="Equals" />
现在GridControl允许您过滤数据,请注意,如果应用排序不兼容的筛选器,则会发生错误。
注意:在本教程中,使用Issues Service作为数据源的示例。
概述
Issues Service允许您应用以下排序顺序:
- Default - 记录从新的到旧的显示
- Hot - 最近几天被关注最多的问题
- Week - 本周浏览量最多的问题
- Created Date(创建日期)
- Votes(投票)
C#
public enum IssueSortOrder {
Default,
Hot,
Week,
CreatedAscending,
CreatedDescending,
VotesAscending,
VotesDescending,
}
在上文中,获取行默认排序顺序:
C#
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
<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#
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);
}
2. 在虚拟源中实现排序:
- 使用FetchEventArgsBase.SortOrder属性获取GridControl的排序。
- 解析排序并返回排序顺序,InfiniteAsyncSource.FetchRows事件处理程序在获取行时考虑了这种排序顺序。
C#
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;
}
}
3. 允许在GridControl中排序:
XAML
<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" />
4. Hot和Week列在GridControl中是不可见的。
将TableView.CompactPanelShowMode 和 TableView.CompactSortElementShowMode属性设置为Always,来允许最终用户指定紧凑面板中的排序顺序:
XAML
<dxg:TableView CompactPanelShowMode="Always" CompactSortElementShowMode="Always" />
下图显示了结果:

更多产品资讯及授权,欢迎来电咨询:023-68661681
更多DevExpress线上公开课、中文教程资讯请上中文网获取
关于慧都科技
慧都是⼀家⾏业数字化解决⽅案公司,专注于软件、⽯油与⼯业领域,以深⼊的业务理解和⾏业经验,帮助企业实现智能化转型与持续竞争优势。
慧都是DevExpress的中国区的合作伙伴,DevExpress作为用户界面领域的优秀产品,帮助企业高效构建权限管理、数据可视化(如网格/图表/仪表盘)、跨平台系统(WinForms/ASP.NET/.NET MAUI)及行业定制解决方案,加速开发并强化交互体验。

欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:DevExpress控件中文网 [https://www.devexpresscn.com/]
本文地址:https://www.devexpresscn.com/post/5618.html
相关产品: DevExpress WPF Subscription, DevExpress Universal Subscription,
联系电话:023-68661681



返回