WinUI | ComponentOne
Controls / DataFilter / Types of Filters / Conditional Filters
In This Topic
    Conditional Filters
    In This Topic

    DataFilter supports conditional filtering operations which can be used to apply filters based on specific conditions. It enables you to highlight critical information, and identify the records that fit a specific condition, when working with a large dataset. With the help of this feature, you can apply conditional filtering on different data types like text, numeric, and date.

    In DataFilter, ConditionalFilter class represents conditional filters. In addition, some more conditional filters are also available in DataFilter that you can use to implement conditional filtering based on the data type of columns. The description of these conditional filters is as follows:

    These classes provide various properties to customize conditional filtering operations for specified data column.

    Set the Initial Filter Operation

    You can set the initial filter operation for various conditional filters such as NumericFilter, DateTimeFilter, TextFilter, etc., by using the DefaultFilterOperation property of the ConditionalFilter class. For example, in case of TextFilter, the default value of the DefaultFilterOperation property is set to Contains. By overriding the default value and setting it to FilterOperation.StartsWith using the DefaultFilterOperation property, users can customize how the filter evaluates text data. This allows for more specific filtering criteria, enabling applications to focus on particular needs, like identifying strings that start with a given prefix. You can set the default value of the DefaultFilterOperation property for both manually created and autogenerated filters.

    Default filter

    In case of autogenerated Text filter, you can easily override the default TextFilter condition Contains with StartsWith condition filter by using the following code. This example uses the same data and code which is used in Types of Filter.

    <c1:C1DataFilter x:Name="c1DataFilter" AutoGenerateFilters="False">
            <c1:C1DataFilter.Filters>
                    <c1:TextFilter PropertyName="Model" DefaultFilterOperation="StartsWith" />
                    <c1:NumericFilter PropertyName="MPG_City" Increment="1" />
                    <c1:FullTextFilter PropertyName="Brand" HeaderText="Brand" />
                    <c1:RangeFilter x:Name="rangeFilter" PropertyName="Price" Maximum="370485" Minimum="12565" Increment="1000"  HeaderText="Price"/>
            </c1:C1DataFilter.Filters>
    </c1:C1DataFilter>
    
    var dataFilter = new C1DataFilter {AutoGenerateFilters = false};
    dataFilter.Filters.Add(new TextFilter {DefaultFilterOperation=FilterOperation.StartsWith,PropertyName = "Brand" });
    LayoutRoot.Children.Add(dataFilter);
    

    However, in case of manually created TextFilter, you can override the default filter condition Contains with StartsWith condition by using the following code:

    C#
    Copy Code
    foreach (var filter in c1DataFilter.Filters) 
    {
       if (filter is TextFilter textFilter)
       {
          textFilter.DefaultFilterOperation = FilterOperation.StartsWith;
       }
    }