xamarin-forms-docs/knowledge-base/autocompleteview-avoid-dupl...

1.5 KiB

title description type page_title slug position tags ticketid res_type
How to avoid adding duplicate tokens to Tokens collection avoid selecting the same suggestion twice how-to How not to select duplicate items in AutoCompleteView autocompleteview-avoid-duplicate-tokens autocompleteview, tokens, duplicates, Xamarin, XamarinForms 1403830 kb

Environment

Product Version 2019.1.318.1
Product AutoCompleteView for Xamarin

Description

What is the best way to avoid adding duplicate tokens to Tokens collection RadAutoCompleteView?

Solution

You could use SuggestionItemSelected event to remove the item - this event is fired before the item is actually added to the Tokens collection, so, if you call directly in its handler the Remove method of the Tokens collection, it will remove the second time the item is added. Here is a sample snippet:

<telerikInput:RadAutoCompleteView x:Name="autoCompleteView"
            ImagePath="ImageSource"
            ItemsSource="{Binding Source}"
            TextSearchPath="Name"
            DisplayMode="Tokens"                               
            SuggestionItemSelected="AutoCompleteView_SuggestionItemSelected"/>

and the event handler:

private void AutoCompleteView_SuggestionItemSelected(object sender, Telerik.XamarinForms.Input.AutoComplete.SuggestionItemSelectedEventArgs e)
{
    autoCompleteView.Tokens.Remove(e.DataItem);
}