AutoCompleteBox CustomFilteringBehavior with Single SelectionMode

4 posts, 0 answers
  1. Toumir
    Toumir avatar
    3 posts
    Member since:
    May 2013

    Posted 12 Oct 2013 Link to this post

    Hi,
    I'm Interest for the AutoCompleteBox  Control but i have this issue:

    Ihave this CustomFilteringBehavior:

    public class CustomFilteringBehavior : FilteringBehavior
        {
            public override System.Collections.Generic.IEnumerable<object> FindMatchingItems(string searchText, System.Collections.IList items, System.Collections.Generic.IEnumerable<object> escapedItems, string textSearchPath, TextSearchMode textSearchMode)
            {
                var searchTextToUpper = searchText.ToUpper();
                var matchingItems = items.OfType<Person>().
                    Where(x => x.FirstName.ToUpper().Contains(searchTextToUpper) || x.LastName.ToUpper().Contains(searchTextToUpper)
                    || (x.FirstName + " " + x.LastName).ToUpper().Contains(searchTextToUpper));
     
                return matchingItems.Where(x => !escapedItems.Contains(x));
            }
        }

    and in my main page i used this CustomFilteringBeahvior:

    <UserControl.Resources>
             
            <local:ViewModel x:Key="ViewModel" />
            <local:CustomFilteringBehavior x:Key="CustomFilteringBehavior" />
             
            <DataTemplate x:Key="CustomItemTemplate">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding FirstName}" />
                    <TextBlock Text=" " />
                    <TextBlock Text="{Binding LastName}" />
                </StackPanel>
            </DataTemplate>
             
        </UserControl.Resources>
     
        <Grid x:Name="LayoutRoot" Background="White" DataContext="{StaticResource ViewModel}">
            <StackPanel>
                <telerik:RadAutoCompleteBox ItemsSource="{Binding People}"
                                            FilteringBehavior="{StaticResource CustomFilteringBehavior}"
                                            DropDownItemTemplate="{StaticResource CustomItemTemplate}"
                                            BoxesItemTemplate="{StaticResource CustomItemTemplate}"
                                            SelectionMode="Single"
                                            AutoCompleteMode="Suggest" />
            </StackPanel>
        </Grid>

    when typing :


    in SelectionMode="Multiple" all works fine but
    in SelectionMode="Single" the result after selection is :


    is there any idea for this behavior ?

    Thank you in advance,
  2. Toumir
    Toumir avatar
    3 posts
    Member since:
    May 2013

    Posted 13 Oct 2013 Link to this post

    Where is the Telerik Team :-(
  3. UI for WPF is Visual Studio 2017 Ready
  4. Kalin
    Admin
    Kalin avatar
    1207 posts

    Posted 14 Oct 2013 Link to this post

    Hi Toumir,

    When the AutoCompleteBox is in Single SelectectionMode it uses a TextBox to display the SelectedItem information as a text by calling its ToString method. So the most simple way to achieve the desired scenario is to override the ToString method in your Person class in order to return the full name of the person instead of the type of the object. Please check the code snippet below:

    public override string ToString()
    {
        return this.FirstName + " " + this.LastName;
    }

    Hope this will work for you. If you have any other questions let us know.

    Regards,
    Kalin
    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 >>
  5. Toumir
    Toumir avatar
    3 posts
    Member since:
    May 2013

    Posted 20 Oct 2013 Link to this post

    :-) Thank you for your answer, this is what I want

Back to Top