Using AutoCompleteBox without diacritics

6 posts, 0 answers
  1. Richard
    Richard avatar
    2 posts
    Member since:
    Jul 2013

    Posted 23 Sep 2013 Link to this post

    Hi,

    is it possible to use autocomplete without diacritics? For example when I have AutoCompleteBox.ItemsSource with ViewModels containing property Name (one contains Name == "Šířka" for example) and I would like to search item with entered word in antocompletebox sir/sirk/Širk/Šíř etc.

    Thank you for your help.
  2. Vladi
    Admin
    Vladi avatar
    744 posts

    Posted 26 Sep 2013 Link to this post

    Hi,

    In the current version of RadAutoCompleteBox control the described behavior when the control searches through its items is not a built-in feature. The control's TextSearchMode could be set to Contains, ContainsCaseSensitive, StartsWith, StartsWithCaseSensitive, the default value is StartsWith. In those modes the searching compares the symbols exactly and do not replace specific symbols with others.

    You could create a custom FilteringBehavior and override its FindMatchingItems method. In that method implement a custom logic that will change the searchText (which is the input text) before passing it to the base.FindMatchingItems() method that returns the matched items. More information about custom FilteringBehavior could be found here.

    Regards,
    Vladi
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Jc
    Jc avatar
    25 posts
    Member since:
    Sep 2012

    Posted 17 Apr 2014 in reply to Vladi Link to this post

    Hello,

    I've got approximately the same problem, about search without accents, I'd like to remove diacritics on the search word, and also on the list of items searched, for example, if I type "greg", i'd like to find "Grégory".
    Would you have any peace of sample code to achieve this?
    Would look something like this:
    public static string RemoveDiacritics(this string s)
            {
                String normalizedString = s.Normalize(NormalizationForm.FormD);
                StringBuilder stringBuilder = new StringBuilder();
     
                for (int i = 0; i < normalizedString.Length; i++)
                {
                    Char c = normalizedString[i];
                    if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
                        stringBuilder.Append(c);
                }
                return stringBuilder.ToString();
            }
     
     
     
     public class WithoutDiacriticsFilteringBehavior : FilteringBehavior
        {
            public override IEnumerable<object> FindMatchingItems(string searchText, IList items, IEnumerable<object> escapedItems, string textSearchPath, TextSearchMode textSearchMode)
            {
                searchText = searchText.ToLower().RemoveDiacritics();
                ///override this to remove diacritics in the search:
                return FindMatchingItemsWithoutDiacritics(searchText, items, escapedItems, textSearchPath, textSearchMode) as IEnumerable<object>;
     
                
            }
        }


    Thank you.
    Kind regards,
    JC



  5. Rosen Vladimirov
    Admin
    Rosen Vladimirov avatar
    640 posts

    Posted 17 Apr 2014 Link to this post

    Hello JC,

    I've created a sample project to show you one way to achieve this. As your ItemsSource will contain items with diacritics, you will have to modify the items collection that is passed to the FindMatchingItems method. In the sample I've used the method you've suggested (RemoveDiacritics) to modify all strings from the items source directly in the FindMatchingItems and when a match is found, I return back the original item from the items source.

    Please give it a try and inform us in case you have any problems or concerns.

    Regards,
    Rosen Vladimirov
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  6. Jc
    Jc avatar
    25 posts
    Member since:
    Sep 2012

    Posted 17 Apr 2014 in reply to Rosen Vladimirov Link to this post

    Your sample seems fine, but in my case, the IList items is not a list of string bue a list of complex objects, a List<User>, searching on the User.FullName. So it won't work for me. I could still implement a specific Behavior for User object, but as I have other types of object, I'd like this behavior to be generic.
    Any idea?
    Thank's for your help.
    KR,
    JC
  7. Rosen Vladimirov
    Admin
    Rosen Vladimirov avatar
    640 posts

    Posted 17 Apr 2014 Link to this post

    Hello JC,

    I've modified my project to use reflection and get the value of the search property based on the TextSearchPath. Please find it attached and inform us in case you have any problems or concerns.

    Regards,
    Rosen Vladimirov
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top
UI for WPF is Visual Studio 2017 Ready