开发者论坛

 找回密码
 注册 (请使用非IE浏览器)
查看: 3780|回复: 0

[Winforms使用技巧教程]掌握Filter Editor(二)

[复制链接]

0

精华

8

贡献

1768

赞扬

特约版主

帖子
583
软币
4524
在线时间
275 小时
注册时间
2019-2-21
发表于 2020-3-25 10:36:43 | 显示全部楼层 |阅读模式

下载DevExpress v19.2完整版  DevExpress v19.2汉化资源获取

DevExpress Winforms Controls 内置140多个UI控件和库,完美构建流畅、美观且易于使用的应用程序。想要体验?点击下载>>

DevExpress WinForms安装附带两个允许最终用户构建过滤器查询的控件:提供GUI的Filter控件和将Filter控件与基于文本输入的面板组合在一起的Filter Editor控件。WinForms中,大多数数据感知控件都使用这些组件,但是您也可以将其包含在自己的表单中,并根据需要将其绑定到数据感知控件中。

Registering函数

自定义函数准备就绪后,您需要对其进行注册,即将其添加到Filter控件和Filter Editor控件支持的函数列表中。如果您在自定义函数类中包括了可选的Register和Unregister方法,则注册代码很短:

//Program.cs file
namespace DXSample {
static class Program {
[STAThread]
static void Main() {
IsWeekendFunction.Register();
WithinDaysOfTodayFunction.Register();
NotBeginsWithFunction.Register();

// ...
Application.Run(new Main());
}
}
}

从技术上讲,您的函数现在可用。如果您在Filter Editor控件的文本面板中手动编辑表达式,并使用这些自定义函数中的任何一个,则将生成有效的过滤条件。 但是到目前为止,这些函数将不会包含在可视化面板中。

根据您的要求,使用以下三种技术之一将自定义函数添加到GUI。

一种特定的控件

若要使一个函数仅可用于一个特定的数据感知控件及其嵌入式Filter Editor控件,请为该控件的QueryCustomFunctions事件实现一个处理程序。使用以下代码,可以在嵌入式Filter Editor和Excel-style过滤器菜单中为数据网格使用IsWeekendFunction,而仅在过滤器编辑器中可见“ InsideDaysOfToday”函数。

gridView1.QueryCustomFunctions += OnQueryCustomFunctions;

private void OnQueryCustomFunctions(object sender,
DevExpress.XtraGrid.Views.Grid.CustomFunctionEventArgs e) {
if(e.PropertyType == typeof(DateTime)) {
e.Add(IsWeekendFunction.FunctionName);
if(e.IsFilterEditor)
e.Add(WithinDaysOfTodayFunction.FunctionName);
}
}

所有Filter和Filter Editor控件

若要注册全局自定义函数来包含在所有Filter和Filter Editor控件中,请将它们添加到事件CriteriaOperator.QueryCustomFunctions的处理程序中。 此示例中全局注册了NotBeginsWith函数:

static class Program {
[STAThread]
static void Main() {
// ...
CriteriaOperator.QueryCustomFunctions += OnQueryCustomUIFunctions;
// ...
}

private static void OnQueryCustomUIFunctions(object sender,
DevExpress.Data.Filtering.CustomFunctionEventArgs e) {
if(e.PropertyType == typeof(string)) {
e.Add(NotBeginsWithFunction.FunctionName);
}
}
}

特定于个别属性

若要注册所有Filter和Filter Editor控件都应该可用但特定于数据类型属性的函数,请使用属性DevExpress.Data.Filtering.CustomFunction注释属性。 在此示例中,数据网格显示具有两个字符串属性Text和Info的类型,自定义函数NotBeginsWith仅适用于Info字段。

[CustomFunction(NotBeginsWithFunction.FunctionName /*, Image = <image>*/)]
public string Info {
get { return info; }
set {
if (info != value) {
info = value;
OnPropertyChanged();
}
}
}


更多产品使用教程,尽在DevExpress中文网哦~

DevExpress技术交流群:540330292      欢迎一起进群讨论



回复

使用道具 举报

Archiver|手机版|小黑屋|开发者网 ( 苏ICP备08004430号-2 )
版权所有:南京韵文教育信息咨询有限公司

GMT+8, 2024-5-2 16:08

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表