DevExpress控件使用交流,DevExpress中国社区Dev联系电话 联系电话:023-68661681

DevExpress Winforms使用技巧教程:掌握Filter Editor(二)

来源:   发布时间:2020-03-24   浏览:1599次

下载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);
}
}

DevExpress Winforms使用技巧教程

所有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 Winforms使用技巧教程

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

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

扫描关注DevExpress中文网微信公众号,及时获取最新动态及最新资讯

DevExpress中文网微信
本站文章除注明转载外,均为本站原创或翻译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:DevExpress控件中文网 [https://www.devexpresscn.com/]
本文地址:https://www.devexpresscn.com/post/1599.html

相关产品: DevExpress Universal Subscription,

在线
客服
微信
QQ 电话
023-68661681
返回
顶部