Edit this page

Filtering Behavior

The main functionality of the RadAutoCompleteBox control is its filtering mechanism which filters the collection that is bound to the ItemsSource of the control based on the input in its TextBox. This article will explain the benefits of the built-in AsyncFilteringBehavior and will demonstrate how to implement a custom FilteringBehavior.

Async Filtering Behavior

When it comes to filtering a huge amount of data the performance is that what really matters. Now with the Q2 2014 release version of UI for WPF, RadAutoCompleteBox provides a new built-in AsyncFilteringBehavior which solves that scenario really easy and makes the filtering of huge amount of items unnoticeable. Unlike the default FilteringBehavior the new AsyncFilteringBehavior performes the filtering operation on a different threads if possible and helps you to achieve a better user experience.

The behavior should be set up the following way:

[XAML] RadAutoCompleteBox with AsyncFilteringBehavior

<telerik:RadAutoCompleteBox>
    <telerik:RadAutoCompleteBox.FilteringBehavior>
        <telerik:AsyncFilteringBehavior />
    </telerik:RadAutoCompleteBox.FilteringBehavior>
</telerik:RadAutoCompleteBox>

Custom Filtering Behavior

Customizing the logic behind this filtering mechanism is a simple task and only requires the creation of a custom FilteringBehavior which could implement any custom filtering logic. The next example will demonstrate how to create a custom FilteringBehavior in order to achieve a functionality that populates the DropDown portion of the control with the entire ItemsSource collection when no match is found.

Before proceeding with this tutorial you should get familiar with Binding To Object.

  1. First you will need to create a custom class that inherits the default FilteringBehavior of the AutoCompleteBox control:

    [C#] Custom class that inherits the default FilteringBehavior

    public class MyCustomFilteringBehavior : FilteringBehavior
    {
    }
    
  2. After that you will need to override its FindMatchingItems() method with a custom logic that will return the entire items collection when no match is found:

    [C#] Overriding FindMatchingItems

    public class MyCustomFilteringBehavior : FilteringBehavior
    {
        public override IEnumerable<object> FindMatchingItems(string searchText, IList items, IEnumerable<object> escapedItems, string textSearchPath, TextSearchMode textSearchMode)
        {
            var result = base.FindMatchingItems(searchText, items, escapedItems, textSearchPath, textSearchMode) as IEnumerable<object>;
    
            if (string.IsNullOrEmpty(searchText) || !result.Any())
            {
                return ((IEnumerable<object>)items).Where(x => !escapedItems.Contains(x));
            }
            return result;
        }
    }
    
  3. Finally all you need to do is set the newly created behavior to the FilteringBehavior of the AutoCompleteBox control. The xaml of the control should look like this:

    [XAML] Setting the newly created behavior

    <telerik:RadAutoCompleteBox ItemsSource="{Binding Countries, Source={StaticResource ViewModel}}"
                                DisplayMemberPath="Name" 
                                TextSearchMode="StartsWith"
                                WatermarkContent="Enter Country Name"
                                FilteringBehavior="{StaticResource CustomFilteringBehavior}"/>
    

You can download a runnable project of the previous example from our online SDK repository here, the example is listed as AutoCompleteBox / NoMatchFilteringBehavior.

The next screenshots show the finally result:

radautocompletebox-features-filteringbehavior-1

radautocompletebox-features-filteringbehavior-2

With the custom FilteringBehavior (the DropDown portion is populated although no watches were found):

radautocompletebox-features-filteringbehavior-3

Without the custom FilteringBehavior (the default FilteringBehavior) (the DropDown portion is not populated because no watch were found):

radautocompletebox-features-filteringbehavior-4

See Also