Searchable List Box For Windows 10 UWP App

I am going to implement an article search that contains the list of articles and which is able to search on the list. While typing in the search box, it will automatically search the item by the article name.

Let’s see the steps.

Create new windows 10 UWP blank app. Then add the search and list box control using the following XAML code.

  1. <StackPanel>  
  2.     <SearchBox x:Name="search" QueryChanged="search_QueryChanged"></SearchBox>  
  3.     <ListBox Background="CornflowerBlue" x:Name="SearchlistBox" FontSize="26">  
  4.         <ListBox.ItemTemplate>  
  5.             <DataTemplate>  
  6.                 <StackPanel Orientation="Horizontal">  
  7.                     <TextBlock Foreground="White" Text="{Binding Name}" FontSize="24" /> </StackPanel>  
  8.             </DataTemplate>  
  9.         </ListBox.ItemTemplate>  
  10.     </ListBox>  
  11. </StackPanel>  
Adding data to the List Box

Go to the code at the back-end file and declare a List type of articles. An article is a class, which contains Name property, and adds the articles to the list box, using the code, given below.
  1. List < Articles > ArticlerList = new List < Articles > ();  
  2. private void MainPage_Loaded(object sender, RoutedEventArgs e)   
  3. {  
  4.     ArticlerList.Add(new Articles()  
  5.     {  
  6.         Name = "Windows 10 UWP"  
  7.     });  
  8.     ArticlerList.Add(new Articles() {  
  9.         Name = "Coding4FunToolkit"  
  10.     });  
  11.     ArticlerList.Add(new Articles() {  
  12.         Name = "Hamburger Menu"  
  13.     });  
  14.     ArticlerList.Add(new Articles() {  
  15.         Name = "Range Selector"  
  16.     });  
  17.     ArticlerList.Add(new Articles() {  
  18.         Name = "Community Toolkit"  
  19.     });  
  20.     ArticlerList.Add(new Articles() {  
  21.         Name = "Play Media File"  
  22.     });  
  23.     ArticlerList.Add(new Articles() {  
  24.         Name = "MVVM Light"  
  25.     });  
  26.     SearchlistBox.ItemsSource = ArticlerList;  
  27. }  
  28. class Articles  
  29. {  
  30.     public string Name   
  31.   {  
  32.         get;  
  33.         set;  
  34.     }  
  35. }  
Here, I am adding data to the list on the page loaded event.

Next, we need to implement search item on search box query changed event.
  1. private void search_QueryChanged(SearchBox sender, SearchBoxQueryChangedEventArgs args)  
  2. {  
  3.     if (ArticlerList != null) {  
  4.         SearchlistBox.ItemsSource = ArticlerList.Where(a => a.Name.ToUpper().Contains(search.QueryText.ToUpper()));  
  5.     }  
  6. }  
I am using LINQ query to search the article item. Here, I am just checking the search text, which is included in the article list. You can customize your search query.

Now, run the Application and enter any text into the search box. The output looks, as shown below.

screen


screen

Up Next
    Ebook Download
    View all
    Learn
    View all