RadAutoCompleBox with custom search function

2 posts, 0 answers
  1. Guy
    Guy avatar
    18 posts
    Member since:
    Oct 2012

    Posted 22 Nov 2012 Link to this post

    Hi

    I have a requirement from my users to be able to type into an autocompletebox and have it suggest results based on multiple values from my business object.

    As an example, if the business object looks like this:
    public class Client
    {
        public string Name {get;set;}
        public string AccountCode {get;set;}
        public string Address {get;set;}
    }

    I want to be able to tell RadAutoCompleteBox to not only search on the Name field, but also the AccountCode when the user types into the box. The item data template I am showing the user is a stack panel containing both fields, so then they can start typing a value from either field to populate the suggestion box.

    If I could provide the control with a search function to use instead of TextSearchPath, that would be ideal. A lamba like this would do the trick:

    radacbox.SearchFunction += (client, search_text) => { client.Name.Contains(search_text) || client.AccountCode.Contains(search_text) };

    Is there any way to override the default search behavior of the RadAutoCompleteBox?
  2. Guy
    Guy avatar
    18 posts
    Member since:
    Oct 2012

    Posted 23 Nov 2012 Link to this post

    Ok, for anyone trying to do this, I solved my own problem. RadAutoCompleteBox has a FilterBehavior member which is based on the IFilteringBehavior interface. Simply create a class which implements that interface, like this:

    internal class CustomRadAutoCompleteBehavior : IFilteringBehavior
        {
            internal CustomRadAutoCompleteBehavior()
            {
                 // custom setup code
            }
     
            public IEnumerable<object> FindMatchingItems(string searchText, System.Collections.IList items, IEnumerable<object> escapedItems, string textSearchPath, TextSearchMode textSearchMode)
            {
                // custom filtering code returning IEnumerable of objects to display
            }
        }

    and set your RadAutoCompleteBox's FilterBehavior to an instance of the new class, like this:

    radBox.FilteringBehavior = new CustomRadAutoCompleteBehavior();

    There you go, now you can filter the autocomplete items however you want, not just on a single string member.

  3. UI for WPF is Visual Studio 2017 Ready
Back to Top