Search code examples
c#xamluwpautosuggest

How to set the text of an Auto Suggest box from c# in Suggestion Chosen event?


I am trying to set the text of Auto Suggest box from code behind in Suggestion Chosen event but by doing that my suggestion list is getting closed and if i set the text directly to the model name then the suggestion list is not getting closed.

I want to set the auto suggest box text to the model properties but the list is closing if i do that,i don't want to close my suggestion list on this event.

On the other hand if i am setting the text as directly the model name then the list is not getting closed.I am literally getting very confused to make it work.

Suggestion Chosen event:

 private void recipient_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)
    {
        var getType = args.SelectedItem.GetType();
        if (getType.Name == "Table_People")
        {
            var selectedItemRecipient = args.SelectedItem as Table_People;
            //By doing this the list is getting closed.
            sender.Text = selectedItemRecipient.FirstName + " " + selectedItemRecipient.LastName;
            recipienterror.Visibility = Visibility.Collapsed;
            _personID = selectedItemRecipient.PersonID;
        }
        else
        {
            var selectedItemRecipientPlaces = args.SelectedItem as Table_Places;
            _placeID = selectedItemRecipientPlaces.PlaceID;
            //By doing this the list is getting closed.
            sender.Text = selectedItemRecipientPlaces.FirstName + " " + selectedItemRecipientPlaces.LastName;
            recipienterror.Visibility = Visibility.Collapsed;
        }

    }

Thanks in advance.


Solution

  • As tushargoyal1309 said that you could set the TextMemberPath property to choose which property from your data object to display in the text box. For more, please refer to AutoSuggestBox class Official documentation.

    <AutoSuggestBox
         x:Name="asb"
         PlaceholderText="Type a name (e.g. John)"
         DisplayMemberPath="DisplayName"
         TextChanged="asb_TextChanged"               
         TextMemberPath="DisplayName"
         QueryIcon="Find"/>