Skip to main content

ExcelStyleFilterElement.QueryOperators Event

Allows you to customize operators in the Excel-style Filter Element.

Namespace: DevExpress.Xpf.Core.FilteringUI

Assembly: DevExpress.Xpf.Grid.v24.1.dll

NuGet Package: DevExpress.Wpf.Grid.Core

Declaration

public event EventHandler<ExcelStyleFilterElementQueryOperatorsEventArgs> QueryOperators

Event Data

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

Property Description
DefaultOperator Gets or sets an operator that is selected when users create a new filter condition. Inherited from QueryOperatorsEventArgsBase<T>.
FieldName Gets a name of the field for which operators are queried. Inherited from QueryOperatorsEventArgsBase<T>.
Operators Gets or sets operators displayed in the operator list.

Remarks

Run Demo: Excel-style Drop-down Filter - Customize the Operator List

The Excel-style Filter Element shows a list of operators the selected field accepts. Use the QueryOperators event to customize the operator list.

The code sample below removes all operators except Equal and Not Equal:

<dxg:GridColumn FieldName="OrderDate">
    <dxg:GridColumn.CustomColumnFilterPopupTemplate>
        <DataTemplate>
            <dxfui:ExcelStyleFilterElement x:Name="PART_FilterElement" QueryOperators="OnExcelStyleFilterQueryOperators"/>
        </DataTemplate>
    </dxg:GridColumn.CustomColumnFilterPopupTemplate>
</dxg:GridColumn>
void OnExcelStyleFilterQueryOperators(object sender, ExcelStyleFilterElementQueryOperatorsEventArgs e) {
    if(e.FieldName == "OrderDate") {
        e.Operators.Clear();
        e.Operators.Add(new ExcelStyleFilterElementOperatorItem(ExcelStyleFilterElementOperatorType.Equal) { Caption = "Equal" });
        e.Operators.Add(new ExcelStyleFilterElementOperatorItem(ExcelStyleFilterElementOperatorType.NotEqual) { Caption = "Not Equal" });
    }
} 

Custom Operators

You can use the QueryOperators event to add custom operators. The code sample below adds the Last Years operator:

  1. Create a custom function. Do one of the following:

    • Use the CustomFunctionFactory.Create method.

      The CustomFunctionFactory is an extension of the DevExpress.Xpf.Grid.v24.1.Extensions.dll library. Refer to C:\Program Files\DevExpress 24.1\Components\Sources\XPF\DevExpress.Xpf.Grid\DevExpress.Xpf.Grid.Extensions\ for information on how extension methods work.

      The CustomFunctionFactory.Create method allows you to create a custom function with a maximum of 4 operands.

    • Implement the ICustomFunctionOperator interface. Refer to the Implementing Custom Functions topic for more information.

    const string CustomFunctionName = "LastYears";
    var currentYear = DateTime.Now.Year;
    
    ICustomFunctionOperatorBrowsable customFunction = CustomFunctionFactory.Create(CustomFunctionName, 
        (DateTime date, int threshold) => {
            return currentYear >= date.Year && currentYear - date.Year <= threshold;
        }
    );
    
  2. Call the CriteriaOperator.RegisterCustomFunction method to register the custom function.

    CriteriaOperator.RegisterCustomFunction(customFunction);
    
  3. Create the ExcelStyleFilterElementOperatorItem and add it to the ExcelStyleFilterElementQueryOperatorsEventArgs.Operators collection. Specify the operator item’s edit settings to define its operands:

    void OnExcelStyleFilterQueryOperators(object sender, ExcelStyleFilterElementQueryOperatorsEventArgs e) {
        if(e.FieldName == "OrderDate") {
            // ...
    
            var customFunctionEditSettings = new BaseEditSettings[] { 
                new TextEditSettings { MaskType = MaskType.Numeric, Mask = "D", MaskUseAsDisplayFormat = true } 
            };
            e.Operators.Add(new ExcelStyleFilterElementOperatorItem(CustomFunctionName, customFunctionEditSettings) { Caption = "Last Years" });
        }
    } 
    

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the QueryOperators 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.

See Also