下面的示例展示了如何使用自定义字符串代替特定筛选的文本表示形式。 在下面的代码中,当筛选 ([Extension] = '.png' OR [Extension] = '.gif') 被应用于视图时,就在筛选面板中呈现为“My Filter”字符串,而不考虑是否在代码中还是由最终用户在运行时刻应用此筛选。
使用 ColumnView.CustomFilterDisplayText 事件,把 ([Extension] = '.png' OR [Extension] = '.gif') 筛选的默认文本表示形式替换为自定义字符串。
ColumnView.CustomFilterDisplayText 事件的 Value 参数指定由 CriteriaOperator 对象表示的当前筛选标准。 在事件处理程序中,必须分析筛选标准,如果找到“[Extension] = '.png' OR [Extension] = '.gif'”筛选条件,则必须把它替换为自定义字符串。
筛选标准 (CriteriaOperator) 是运算符和操作数的树。 为了分析它,使用了一个自定义的 MyDisplayCriteriaGenerator 类,该类派生于 DisplayCriteriaGenerator 帮助器类。 MyDisplayCriteriaGenerator 类重写了为筛选标准树的每个元素都发生的 Visit 方法。 Visit 方法的实现是十分清晰的: 确定当前处理的树的元素是否为所需的筛选条件,如果是,则返回自定义显示文本 (“My Filter”)。 注意,一个新的 OperandValue 对象被创建,以返回自定义字符串 (OperandValue 对象是树的元素)。 此对象的 ToString() 方法简单返回“My Filter”字符串,此字符串将在适当的时间和屏幕位置显示。
C# | 复制代码 |
---|---|
using DevExpress.XtraGrid.Views.Base; using DevExpress.XtraGrid.Columns; using DevExpress.Data.Filtering; using DevExpress.XtraGrid.FilterEditor; using DevExpress.XtraEditors.Controls; using DevExpress.XtraEditors.Filtering; private void gridView1_CustomFilterDisplayText(object sender, ConvertEditValueEventArgs e) { CriteriaOperator filter = e.Value as CriteriaOperator; if (!ReferenceEquals(filter, null)) { e.Value = MyDisplayCriteriaGenerator.Process(gridView1, filter); e.Handled = true; } } public class MyDisplayCriteriaGenerator : DisplayCriteriaGenerator { static CriteriaOperator predefinedCriteria = CriteriaOperator.Parse("[Extension] = '.png' OR [Extension] = '.gif'"); static CriteriaOperator predefinedCriteria2 = CriteriaOperator.Parse("[Extension] = '.gif' OR [Extension] = '.png'"); public static CriteriaOperator Process(ColumnView view, CriteriaOperator filter) { return new MyDisplayCriteriaGenerator( new ViewFilterColumnCollection(view)).Process(filter); } public MyDisplayCriteriaGenerator(FilterColumnCollection columns) : base(columns) { } public override object Visit(GroupOperator theOperator) { if (Equals(theOperator, predefinedCriteria) || Equals(theOperator, predefinedCriteria2)) return new OperandValue("My Filter"); return base.Visit(theOperator); } } |
Visual Basic | 复制代码 |
---|---|
Imports DevExpress.XtraGrid.Views.Base Imports DevExpress.XtraGrid.Columns Imports DevExpress.Data.Filtering Imports DevExpress.XtraGrid.FilterEditor Imports DevExpress.XtraEditors.Controls Imports DevExpress.XtraEditors.Filtering; Private Sub GridView1_CustomFilterDisplayText(ByVal sender As System.Object, _ ByVal e As ConvertEditValueEventArgs) Handles GridView1.CustomFilterDisplayText Dim filter As CriteriaOperator = CType(e.Value, CriteriaOperator) If Not ReferenceEquals(filter, Nothing) Then e.Value = MyDisplayCriteriaGenerator.Process(GridView1, filter) e.Handled = True End If End Sub Public Class MyDisplayCriteriaGenerator Inherits DisplayCriteriaGenerator Private predefinedCriteria As CriteriaOperator = _ CriteriaOperator.Parse("[Extension] = '.png' OR [Extension] = '.gif'") Private predefinedCriteria2 As CriteriaOperator = _ CriteriaOperator.Parse("[Extension] = '.gif' OR [Extension] = '.png'") Public Overloads Shared Function Process(ByVal view As ColumnView, _ ByVal filter As CriteriaOperator) As CriteriaOperator Return New MyDisplayCriteriaGenerator( _ New ViewFilterColumnCollection(view)).Process(filter) End Function Public Sub New(ByVal columns As FilterColumnCollection) MyBase.New(columns) End Sub Public Overrides Function Visit(ByVal theOperator As GroupOperator) As Object If Equals(theOperator, predefinedCriteria) OrElse _ Equals(theOperator, predefinedCriteria2) Then Return New OperandValue("My Filter") End If Return MyBase.Visit(theOperator) End Function End Class |