Back to Devexpress

GridControl.SubstituteFilter Event

wpf-devexpress-dot-xpf-dot-grid-dot-gridcontrol-480cb9cd.md

latest9.0 KB
Original Source

GridControl.SubstituteFilter Event

Allows you to replace a filter applied with another filter.

Namespace : DevExpress.Xpf.Grid

Assembly : DevExpress.Xpf.Grid.v25.2.dll

NuGet Package : DevExpress.Wpf.Grid.Core

Declaration

csharp
public event EventHandler<SubstituteFilterEventArgs> SubstituteFilter
vb
Public Event SubstituteFilter As EventHandler(Of SubstituteFilterEventArgs)

Event Data

The SubstituteFilter event's data class is SubstituteFilterEventArgs. The following properties provide information specific to this event:

PropertyDescription
FilterGets or sets the filter applied to a data control.

Remarks

The currently applied filter is specified by the Filter event parameter. To replace this filter, assign a new filter criterion to this parameter.

The following code sample demonstrates how to handle the SubstituteFilter event to filter grid values that include diacritic characters:

csharp
using DevExpress.Data.Filtering;
using DevExpress.Data.Filtering.Helpers;
using System;
using System.Globalization;
using System.Linq;
using System.Text;

namespace DXSample {
    public partial class MainWindow {
        public MainWindow() {
            InitializeComponent();
        }
        static MainWindow() {
            CriteriaOperator.RegisterCustomFunction(new CustomOperator());
        }
        private void grid_SubstituteFilter(object sender, DevExpress.Data.SubstituteFilterEventArgs e) {
            e.Filter = CustomPatcher.Patch(e.Filter);
        }
    }
    public class CustomPatcher : ClientCriteriaLazyPatcherBase.AggregatesCommonProcessingBase {
        private CustomPatcher() { }
        public static CriteriaOperator Patch(CriteriaOperator source) {
            return new CustomPatcher().Process(source);
        }
        public override CriteriaOperator Visit(FunctionOperator theOperator) {
            if (theOperator.OperatorType == FunctionOperatorType.Contains
                && theOperator.Operands[0] is OperandProperty property
                && theOperator.Operands[1] is OperandValue value
                && value.Value is string)
                return new FunctionOperator(FunctionOperatorType.Contains,
                                            new FunctionOperator("RemoveDiacriticsCustom", new OperandProperty(property.PropertyName)),
                                            new FunctionOperator("RemoveDiacriticsCustom", new OperandValue(value.Value)));
            return base.Visit(theOperator);
        }
    }
    public class CustomOperator : ICustomFunctionOperator {
        string ICustomFunctionOperator.Name { get { return "RemoveDiacriticsCustom"; } }
        private static string RemoveDiacriticsCustom(string text) {
            return string.Concat(text.Normalize(NormalizationForm.FormD).Where(ch => CharUnicodeInfo.GetUnicodeCategory(ch) != UnicodeCategory.NonSpacingMark)).Normalize(NormalizationForm.FormC);
        }
        object ICustomFunctionOperator.Evaluate(params object[] operands) {
            if (operands.Length == 1 && operands[0] is string value)
                return RemoveDiacriticsCustom(value);
            return null;
        }
        Type ICustomFunctionOperator.ResultType(params Type[] operands) {
            return typeof(string);
        }
    }
}
vb
Imports System.Globalization
Imports System.Text
Imports DevExpress.Data.Filtering
Imports DevExpress.Data.Filtering.Helpers

Namespace BindToLocalCollection
    Partial Public Class MainWindow
        Public Sub New()
            InitializeComponent()
        End Sub
        Shared Sub New()
            CriteriaOperator.RegisterCustomFunction(New CustomOperator())
        End Sub

        Private Sub grid_SubstituteFilter(ByVal sender As Object, ByVal e As DevExpress.Data.SubstituteFilterEventArgs)
            e.Filter = CustomPatcher.Patch(e.Filter)
        End Sub
    End Class
    Public Class CustomPatcher
        Inherits ClientCriteriaLazyPatcherBase.AggregatesCommonProcessingBase

        Private Sub New()
        End Sub
        Public Shared Function Patch(ByVal source As CriteriaOperator) As CriteriaOperator
            Return (New CustomPatcher()).Process(source)
        End Function
        Public Overrides Function Visit(ByVal theOperator As FunctionOperator) As CriteriaOperator
            If theOperator.OperatorType = FunctionOperatorType.Contains AndAlso TypeOf theOperator.Operands(0) Is OperandProperty AndAlso TypeOf theOperator.Operands(1) Is OperandValue AndAlso TypeOf CType(theOperator.Operands(1), OperandValue).Value Is String Then
                Return New FunctionOperator(FunctionOperatorType.Contains, New FunctionOperator("RemoveDiacriticsCustom", New OperandProperty(CType(theOperator.Operands(0), OperandProperty).PropertyName)), New FunctionOperator("RemoveDiacriticsCustom", New OperandValue(CType(theOperator.Operands(1), OperandValue).Value)))
            End If
            Return MyBase.Visit(theOperator)
        End Function
    End Class
    Public Class CustomOperator
        Implements ICustomFunctionOperator

        Private ReadOnly Property ICustomFunctionOperator_Name() As String Implements ICustomFunctionOperator.Name
            Get
                Return "RemoveDiacriticsCustom"
            End Get
        End Property
        Private Shared Function RemoveDiacriticsCustom(ByVal text As String) As String
            Return String.Concat(text.Normalize(NormalizationForm.FormD).Where(Function(ch) CharUnicodeInfo.GetUnicodeCategory(ch) <> UnicodeCategory.NonSpacingMark)).Normalize(NormalizationForm.FormC)
        End Function
        Private Function ICustomFunctionOperator_Evaluate(ParamArray ByVal operands() As Object) As Object Implements ICustomFunctionOperator.Evaluate
            If operands.Length = 1 AndAlso TypeOf operands(0) Is String Then
                Return RemoveDiacriticsCustom(DirectCast(operands(0), String))
            End If
            Return Nothing
        End Function
        Private Function ICustomFunctionOperator_ResultType(ParamArray ByVal operands() As Type) As Type Implements ICustomFunctionOperator.ResultType
            Return GetType(String)
        End Function
    End Class
End Namespace

Do not modify the existing filter object assigned to the Filter event parameter.

Refer to the following help topic for more information on criteria patchers: Traverse Through and Modify the CriteriaOperator Instances.

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the SubstituteFilter event.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

wpf-data-grid-filter-columns-bound-to-collection-properties/CS/FilterDropDown_AgregateOperators/Behaviors/FilterDropDownAggregateOperatorBehavior.cs#L41

csharp
AssociatedObject.Loaded -= AssociatedObject_Loaded;
AssociatedObject.SubstituteFilter += AssociatedObject_SubstituteFilter;
AssociatedObject.View.ShowFilterPopup += View_ShowFilterPopup;

wpf-data-grid-filter-columns-bound-to-collection-properties/VB/FilterDropDown_AgregateOperators/Behaviors/FilterDropDownAggregateOperatorBehavior.vb#L54

vb
RemoveHandler AssociatedObject.Loaded, AddressOf AssociatedObject_Loaded
AddHandler AssociatedObject.SubstituteFilter, AddressOf AssociatedObject_SubstituteFilter
AddHandler AssociatedObject.View.ShowFilterPopup, AddressOf View_ShowFilterPopup

See Also

GridControl Class

GridControl Members

DevExpress.Xpf.Grid Namespace