AutoSuggestBox In UWP

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,

  • Text Changed: Fires when user enters text, it updates the suggestion list.
  • Suggestion Chosen: When choose suggestion in suggested list, update the text box.
  • Query Submitted: When submits a query, it shows the query result.

Getting Stated:

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

  • Start Visual Studio 2015
  • Create a new project
  • Click Visual C# and Windows, then select Universal
  • Click on Blank App (Universal Windows)
  • Provide the name and location of project
  • Click OK

First drag and drop AutoSuggestBox control on page from toolbox.

  1. <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  2.   
  3.     <AutoSuggestBox HorizontalAlignment="Left" x:Name="txtAutoSuggestBox" TextChanged="AutoSuggestBox_TextChanged" QuerySubmitted="AutoSuggestBox_QuerySubmitted" SuggestionChosen="AutoSuggestBox_SuggestionChosen" QueryIcon="Find" PlaceholderText="Search" VerticalAlignment="Top" Width="294" />  
  4.   
  5. </Grid>     
  1. public sealed partial class MainPage: Page  
  2.   
  3. {  
  4.     public MainPage()  
  5.     {  
  6.   
  7.         suggestions = new ObservableCollection < string > ();  
  8.   
  9.         this.InitializeComponent();  
  10.   
  11.     }  
  12.   
  13.     private ObservableCollection < String > suggestions;  
  14.   
  15.     private void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)  
  16.   
  17.     {  
  18.   
  19.         if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)  
  20.   
  21.         {  
  22.   
  23.             suggestions.Clear();  
  24.   
  25.             suggestions.Add(sender.Text + "1");  
  26.   
  27.             suggestions.Add(sender.Text + "2");  
  28.   
  29.             suggestions.Add(sender.Text + "3");  
  30.   
  31.             suggestions.Add(sender.Text + "4");  
  32.   
  33.             suggestions.Add(sender.Text + "5");  
  34.   
  35.             suggestions.Add(sender.Text + "6");  
  36.   
  37.             suggestions.Add(sender.Text + "7");  
  38.   
  39.             suggestions.Add(sender.Text + "8");  
  40.   
  41.             suggestions.Add(sender.Text + "9");  
  42.   
  43.             sender.ItemsSource = suggestions;  
  44.   
  45.         }  
  46.   
  47.     }  
  48.   
  49.     private void AutoSuggestBox_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)  
  50.   
  51.     {  
  52.   
  53.         if (args.ChosenSuggestion != null)  
  54.   
  55.             txtAutoSuggestBox.Text = args.ChosenSuggestion.ToString();  
  56.   
  57.         else  
  58.   
  59.             txtAutoSuggestBox.Text = sender.Text;  
  60.   
  61.     }  
  62.   
  63.     private void AutoSuggestBox_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)  
  64.   
  65.     {  
  66.   
  67.         txtAutoSuggestBox.Text = "Choosen";  
  68.   
  69.     }  
  70.   
  71. }  
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:


Similar Articles