AutoSuggestBox provides a list suggestions based on what user type. Suggestions works when text has been changed by the user and take care of providing relevant suggestions for the control to display.

Three type of actions mostly use in AutoSuggestBox,

Getting Stated:

Here are the steps to get started with Windows Universal App:

First drag and drop AutoSuggestBox control on page from toolbox.

  1. <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
  2. <AutoSuggestBox HorizontalAlignment="Left" x:Name="txtAutoSuggestBox" TextChanged="AutoSuggestBox_TextChanged" QuerySubmitted="AutoSuggestBox_QuerySubmitted" SuggestionChosen="AutoSuggestBox_SuggestionChosen" QueryIcon="Find" PlaceholderText="Search" VerticalAlignment="Top" Width="294" />
  3. </Grid>
  1. public sealed partial class MainPage: Page
  2. {
  3. public MainPage()
  4. {
  5. suggestions = new ObservableCollection < string > ();
  6. this.InitializeComponent();
  7. }
  8. private ObservableCollection < String > suggestions;
  9. private void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
  10. {
  11. if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
  12. {
  13. suggestions.Clear();
  14. suggestions.Add(sender.Text + "1");
  15. suggestions.Add(sender.Text + "2");
  16. suggestions.Add(sender.Text + "3");
  17. suggestions.Add(sender.Text + "4");
  18. suggestions.Add(sender.Text + "5");
  19. suggestions.Add(sender.Text + "6");
  20. suggestions.Add(sender.Text + "7");
  21. suggestions.Add(sender.Text + "8");
  22. suggestions.Add(sender.Text + "9");
  23. sender.ItemsSource = suggestions;
  24. }
  25. }
  26. private void AutoSuggestBox_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
  27. {
  28. if (args.ChosenSuggestion != null)
  29. txtAutoSuggestBox.Text = args.ChosenSuggestion.ToString();
  30. else
  31. txtAutoSuggestBox.Text = sender.Text;
  32. }
  33. private void AutoSuggestBox_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)
  34. {
  35. txtAutoSuggestBox.Text = "Choosen";
  36. }
  37. }
Output:

Enter a number in text box then suggestions also displaying.

Conclusion:

For more information, download attached source code of this sample. If any question, then please leave a comment in C# corner comment section.

Read more articles on Universal Windows Apps: