翻譯|使用教程|編輯:龔雪|2020-03-12 09:09:36.963|閱讀 738 次
概述:DevExpress WinForms安裝附帶兩個(gè)允許最終用戶構(gòu)建過(guò)濾器查詢的控件:提供GUI的Filter控件和將Filter控件與基于文本輸入的面板組合在一起的Filter Editor控件。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
下載DevExpress v19.2完整版 DevExpress v19.2漢化資源獲取
DevExpress Winforms Controls 內(nèi)置140多個(gè)UI控件和庫(kù),完美構(gòu)建流暢、美觀且易于使用的應(yīng)用程序。想要體驗(yàn)?點(diǎn)擊下載>>
DevExpress WinForms安裝附帶兩個(gè)允許最終用戶構(gòu)建過(guò)濾器查詢的控件:提供GUI的Filter控件和將Filter控件與基于文本輸入的面板組合在一起的Filter Editor控件。WinForms中,大多數(shù)數(shù)據(jù)感知控件都使用這些組件,但是您也可以將其包含在自己的表單中,并根據(jù)需要將其綁定到數(shù)據(jù)感知控件中。
為了說(shuō)明這一點(diǎn),下面是帶有Filter Editor的數(shù)據(jù)網(wǎng)格,用戶可以單擊過(guò)濾器面板中的Edit Filter按鈕來(lái)調(diào)出Filter Editor,并且由于屬性DefaultFilterEditorView設(shè)置為TextAndVisual,因此可以看到Filter Editor Control的文本面板。
在下圖中,您可以看到兩個(gè)控件中可用的一些標(biāo)準(zhǔn)功能,包括“小于或等于”,“大于或等于”,“今天”,“昨天”以及許多其他功能。Filter控件和Filter Editor控件均提供多種功能供您選擇,可用功能集因要為其構(gòu)建表達(dá)式的數(shù)據(jù)字段的類型而異。
在某些情況下,標(biāo)準(zhǔn)函數(shù)集還不夠。技術(shù)團(tuán)隊(duì)在處理大量技術(shù)支持問(wèn)題發(fā)現(xiàn),查找是最常見需要的自定義函數(shù)。以下是三種最受歡迎的方案:
從v19.1開始,F(xiàn)ilter Editor控件和Filter控件完全支持自定義函數(shù),從而可以輕松實(shí)現(xiàn)上述方案和許多其他方案。
技術(shù)基礎(chǔ)
自定義函數(shù)是實(shí)現(xiàn)接口ICustomFunctionDisplayAttributes的類,請(qǐng)注意如果需要服務(wù)器端對(duì)自定義函數(shù)的處理,則可以額外實(shí)現(xiàn)ICustomFunctionOperatorFormattable接口,但是在本文范圍內(nèi),我們僅關(guān)注ICustomFunctionDisplayAttributes。
這些是接口實(shí)現(xiàn)所需的方法和屬性:
最后,我們建議添加兩個(gè)靜態(tài)便利函數(shù)Register和Unregister。 這是一個(gè)可選步驟,但是實(shí)現(xiàn)很簡(jiǎn)單(請(qǐng)參見下文),并且它們以CriteriaOperator類型調(diào)用現(xiàn)有的幫助器。
示例
供您參考,下面是三個(gè)示例,它們涵蓋了上面提到的三個(gè)最需要的方案。 第一個(gè)自定義函數(shù)稱為NotBeginsWith,它是對(duì)標(biāo)準(zhǔn)函數(shù)BeginsWith的取反。
public class NotBeginsWithFunction : ICustomFunctionDisplayAttributes { public const string FunctionName = "NotBeginsWith"; static readonly NotBeginsWithFunction instance = new NotBeginsWithFunction(); public static void Register() { CriteriaOperator.RegisterCustomFunction(instance); } public static bool Unregister() { return CriteriaOperator.UnregisterCustomFunction(instance); } public string Name => FunctionName; public string DisplayName => "Does not begin with"; public object Image => "FontSizeDecrease;Office2013"; public string Description => "Hides records when the field begins with the given value"; public FunctionCategory Category => FunctionCategory.Text; public int MinOperandCount => 2; public int MaxOperandCount => 2; public bool IsValidOperandCount(int count) => count == 2; public bool IsValidOperandType(int operandIndex, int operandCount, Type type) => type == typeof(string); public Type ResultType(params Type[] operands) => typeof(bool); public object Evaluate(params object[] operands) { if(operands[0] != null && operands[1] != null) { string str1 = operands[0].ToString(); string str2 = operands[1].ToString(); return !str1.StartsWith(str2, StringComparison.InvariantCultureIgnoreCase); } return false; } }
這是第二個(gè)自定義函數(shù)InternalDaysOfToday,用于檢查DateTime值是否在今天-N天和今天+ N天的時(shí)間范圍內(nèi)。
public class WithinDaysOfTodayFunction : ICustomFunctionDisplayAttributes { public const string FunctionName = "WithinDaysOfToday"; static readonly WithinDaysOfTodayFunction instance = new WithinDaysOfTodayFunction(); public static void Register() { CriteriaOperator.RegisterCustomFunction(instance); } public static bool Unregister() { return CriteriaOperator.UnregisterCustomFunction(instance); } public string Name => FunctionName; public string DisplayName => "Within days of today"; public object Image => "SwitchTimeScalesTo;Size16x16;Colored"; public string Description => "Shows records when the field value within X days of today"; public FunctionCategory Category => FunctionCategory.DateTime; public int MinOperandCount => 2; public int MaxOperandCount => 2; public bool IsValidOperandCount(int count) => count == 2; public bool IsValidOperandType(int operandIndex, int operandCount, Type type) => operandIndex == 0 && type == typeof(DateTime) || operandIndex == 1 && type == typeof(int); public Type ResultType(params Type[] operands) => return typeof(bool); public object Evaluate(params object[] operands) { DateTime dt = Convert.ToDateTime(operands[0]); int days = Convert.ToInt32(operands[1]); DateTime start = DateTime.Today.AddDays(-days); DateTime end = DateTime.Today.AddDays(days); return dt >= start && dt <= end; } }
最后,IsWeekend測(cè)試DateTime值是星期六還是星期天。
public class IsWeekendFunction : ICustomFunctionDisplayAttributes { public const string FunctionName = "IsWeekend"; static readonly IsWeekendFunction instance = new IsWeekendFunction(); public static void Register() { CriteriaOperator.RegisterCustomFunction(instance); } public static bool Unregister() { return CriteriaOperator.UnregisterCustomFunction(instance); } public string Name => FunctionName; public string DisplayName => "Is weekend"; public object Image => "DayView;Office2013"; public string Description => "Shows records when the field value is on Saturday or Sunday"; public FunctionCategory Category => FunctionCategory.DateTime; public int MinOperandCount => 1; public int MaxOperandCount => 1; public bool IsValidOperandCount(int count) => count == 1; public bool IsValidOperandType(int operandIndex, int operandCount, Type type) => type == typeof(DateTime); public Type ResultType(params Type[] operands) => typeof(bool); public object Evaluate(params object[] operands) { DateTime dt = Convert.ToDateTime(operands[0]); return dt.DayOfWeek == DayOfWeek.Sunday || dt.DayOfWeek == DayOfWeek.Saturday; } }
DevExpress v19.2線上公開課即將開課,前10名免費(fèi)參與哦~
DevExpress技術(shù)交流群:540330292 歡迎一起進(jìn)群討論
掃描關(guān)注DevExpress中文網(wǎng)微信公眾號(hào),及時(shí)獲取最新動(dòng)態(tài)及最新資訊
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)