Getting Started With Auto Complete Box in WP8


This article explains the "auto complete box" control of the Windows Phone toolkit. The auto complete box is a combination of two controls, One is used for input and that is a text box, the second one is used for selecting the item and that is similar to a list box. In this article I'll cover the basics of the "auto complete box" control. After reading this article you will be familiar with the "auto complete box" control and how to add it to your app or an existing project. Let's start with the description of this control.

Auto Complete Box Control

As the name indicates, it's a control that provides an auto text completion capability to user on the basis of predefined collections. This control is a combination of text box and list box. The text box is where the user types the text and the list box works as a container of items that can be selected for completing the text box value. The list box contains the items that are filtered on the basis of input. The list box is known as a drop down since it opens only when there is an item to select.

Some of the properties of the Auto Complete Box that I have used in this demo are as follows:

  • Text

    This property is used for setting the default text in a text box of the auto complete box.
  • Filter Mode

    A very important and useful one. It allows you to set the type of filtering you want on collections. Some of the filters are like contains, contains case sensitive, equal and so on.

Apart from the preceding properties the following two events are also used.

  1. Selection changed

    This event is fired when the item selected in the drop down is changed.
  2. Got Focus

    This event is fired when the text box gets the focus.

Before you can run the following demo you need to install a Windows Phone 8 toolkit in your project. It's very easy to do this.

Installing Windows Phone 8 Toolkit

Use the following procedure to install the Windows Phone 8 toolkit using Nugget.

  1. Open your app project (either an existing one or a new one). 
  2. From the toolbar select "Tools" -> "NuGet Package Manager" -> "Package Manager Console". 
  3. Now in the Package Manager Console type the following:

    Install-Package WPtoolkit
  4. After typing this you will probably see the log similar to this:

    install-package wptoolkit
    'WPtoolkit 4.2013.08.16' already installed.
  5. Adding 'WPtoolkit 4.2013.08.16' to Demo.
  6. Successfully added 'WPtoolkit 4.2013.08.16' to Demo. 
  7. The next step is to add its reference to the XAML page. To do this just add this line:


The following demo demonstrate the basic code required for adding the auto complete box in any project. The code is very simple to follow and works in the following steps:

  • Add the auto complete box control to your design file.
  • Provide whatever name you want to the control. This name will be used from the code behind for referring and controlling its behavior.
  • Now move to the code behind. In the page constructor we have the "startDemo" function that basically initiates the demo.
  • Inside the start demo we have the "createRandomItemSource" function that prepares a collection of randomly generated strings. These strings serve the purpose of an auto completion source for the auto completion box control.  
  • The Selection change event is used for fetching the selected item.
  • The Search text is text being typed by the user in the text box and it is retrieved by the Search text property.

    Let's see how to code it.


    1. <phone:PhoneApplicationPage  
    2.     x:Class="Demo.MainPage"  
    3.     xmlns=""  
    4.     xmlns:x=""  
    5.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"  
    6.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"  
    7.     xmlns:d=""  
    8.     xmlns:mc=""  
    9.     xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"  
    10.     mc:Ignorable="d"  
    11.     FontFamily="{StaticResource PhoneFontFamilyNormal}"  
    12.     FontSize="{StaticResource PhoneFontSizeNormal}"  
    13.     Foreground="{StaticResource PhoneForegroundBrush}"  
    14.     SupportedOrientations="Portrait" Orientation="Portrait"  
    15.     shell:SystemTray.IsVisible="True">  
    17.     <!--LayoutRoot is the root grid where all page content is placed-->  
    18.     <Grid x:Name="LayoutRoot" Background="Transparent">  
    19.         <Grid.RowDefinitions>  
    20.             <RowDefinition Height="768"/>  
    21.         </Grid.RowDefinitions>  
    23.         <!--TitlePanel contains the name of the application and page title-->  
    24.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,512">  
    25.             <TextBlock Text="Demo" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>  
    26.             <TextBlock  Text="Demo" Margin="9,-7,0,0" FontSize="40" />  
    27.         </StackPanel>  
    29.         <!--ContentPanel - place additional content here-->  
    30.         <StackPanel Margin="0,256,0,437" >  
    31.             <toolkit:AutoCompleteBox x:Name="acb" SelectionChanged="acb_SelectionChanged"  Text="Start Typing..." GotFocus="acb_GotFocus" FilterMode="Contains" />  
    32.         </StackPanel>  
    33.         <TextBlock Text="Selected Item:" FontSize="25" Margin="0,519,307,214"></TextBlock>  
    34.         <TextBlock Name="selectedText" Text="None" FontSize="25" Margin="178,519,0,214"></TextBlock>  
    35.         <TextBlock Text="Searched Text:" FontSize="25" Margin="0,559,307,174"/>  
    36.         <TextBlock x:Name="searchText" FontSize="25" Text="None" Margin="178,559,0,174"/>  
    37.     </Grid>  
    38. </phone:PhoneApplicationPage> 

    C# Code Behind

    1. using Microsoft.Phone.Controls;  
    2. using System;  
    3. using System.Collections.Generic;  
    4. using System.Linq;  
    5. using System.Net;  
    6. using System.Windows;  
    7. using System.Windows.Controls;  
    8. using System.Diagnostics;  
    9. using Microsoft.Phone.Tasks;  
    10. using System.IO;  
    12. namespace Demo  
    13. {  
    14.     public partial class MainPage : PhoneApplicationPage  
    15.     {  
    16.         // Constructor  
    17.         public MainPage()  
    18.         {  
    19.             InitializeComponent();  
    20.             startDemo();  
    21.         }  
    23.         List<string> textList = new List<string>();  
    25.         private void startDemo()  
    26.         {  
    27.             createRandomItemSource();  
    29.         }  
    31.         private void createRandomItemSource()  
    32.         {  
    33.             for (int i = 0; i < 50; i++)  
    34.             {  
    35.                 textList.Add(getRandomText());  
    36.             }  
    38.             acb.ItemsSource = textList;  
    39.         }  
    40.         Random random = new Random();  
    41.         private string getRandomText()  
    42.         {  
    43.             var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";  
    44.             var result = new string(  
    45.                 Enumerable.Repeat(chars, 8)  
    46.                           .Select(s => s[random.Next(s.Length)])  
    47.                           .ToArray());  
    48.             return result.ToLower();  
    49.         }  
    51.         private void acb_SelectionChanged(object sender, SelectionChangedEventArgs e)  
    52.         {  
    53.             if (acb.SelectedItem !=null)  
    54.             {  
    55.                 selectedText.Text = acb.SelectedItem.ToString();  
    56.                 searchText.Text = acb.SearchText;                  
    57.             }  
    58.         }  
    60.         private void acb_GotFocus(object sender, RoutedEventArgs e)  
    61.         {  
    62.             acb.Text = "";  
    63.         }    
    64.     }