Namespaces
Variants
Actions

How to use Auto Complete Box in Windows Phone 7

Jump to: navigation, search

This article shows how to use AutoCompleteBox, a Windows Phone 7 control that provides a text box for user input and a drop-down that contains possible matches based on the input in the text box.

Article Metadata

Code Example
Compatibility
Platform(s): Windows Phone 7.5

Article
Created: pooja_1650 (24 Jan 2012)
Last edited: hamishwillee (26 Apr 2012)

Ui AutoCompleteBoxTool.PNG

Contents

Pre-requisites

The AutoCompleteBox is defined in the Silverlight Toolkit, which you can download from here. If the toolkit is not installed already, please follow the installation instructions below.

Installing the Silverlight Toolkit

  • Download the .msi file from http://silverlight.codeplex.com/releases/view/60291
  • After downloading the .msi file, double click it to install it. After successful installation re-start the Visual Studio 2010. (You may perform this step before creating a new project as well)
  • To cross check the installation of Toolkit, hover ToolBox >>right click on 'Windows Phone Controls' >> Choose Items. Now you should be able to see 'AutoCompleteBox' in the tool list.
    AutoCompleteBox tool in the tools list

Starting a New Project

  • Create a new project in Visual Studio 2010 from File>>New Project>>Windows Phone Application - Visual C#. Give a desired name of the project.

Creating a new Project in Windows Phone 7 using Visual C#

Using IEnumerable

  • We now need to make a class derived from IEnumerable interface. For this, right click the project in Project Explorer window: >>Add>>New Item>>Class (Here, we are using 'SampleWords' as our class name).
  • If we derive any class from IEnumerable interface, then it's compulsory to define the function GetEnumerator(). So, our class should look like below:
    namespace SampleAutoCompleteBox
    {
    public class SampleWords : IEnumerable
    {
    public IEnumerable AutoCompletions = new List<string>()
    {
    "Lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing", "elit", "Nullam", "felis", "dui", "gravida", "at",
    "condimentum", "eget", "mattis", "non", "est", "Duis", "porta", "ornare", "tellus", "at", "convallis", "nibh", "aliquam",
    "faucibus", "Vivamus", "molestie", "fringilla", "ullamcorper", "Aenean", "non", "diam", "eu", "sapien", "pretium", "iaculis",
    "Quisque", "at", "ante", "libero", "eu", "tincidunt", "urna", "Cras", "libero", "ligula", "hendrerit", "at", "posuere", "at",
    "tempor", "at", "nulla", "Aliquam", "feugiat", "sagittis", "dolor", "convallis", "porttitor", "neque", "commodo", "ut", "Praesent",
    "egestas", "tincidunt", "lectus", "et", "pharetra", "enim", "semper", "et", "Fusce", "placerat", "orci", "vel", "iaculis",
    "dictum", "nulla", "sem", "convallis", "nunc", "in", "viverra", "leo", "mauris", "eu", "odio", "Nullam", "et", "ultricies",
    "sapien", "Proin", "quis", "mi", "a", "sapien", "semper", "lobortis", "ut", "eget", "est", "Suspendisse", "scelerisque", "porta",
    "mattis", "In", "eleifend", "tellus", "vel", "nulla", "aliquam", "ornare", "Praesent", "tincidunt", "dui", "ut", "libero",
    "iaculis", "consequat", "Nunc", "interdum", "eleifend", "rhoncus", "Curabitur", "sollicitudin", "nulla", "sagittis", "quam",
    "vehicula", "cursus", "Fusce", "laoreet", "arcu", "vitae", "fringilla", "scelerisque", "nisi", "purus", "laoreet", "ipsum", "id",
    "suscipit", "erat", "tellus", "eu", "sapien", "Proin", "pharetra", "tortor", "nisl", "Etiam", "et", "risus", "eget", "lectus",
    "vulputate", "dignissim", "ac","sed", "erat", "Nulla", "vel", "condimentum", "nunc", "Suspendisse", "aliquam", "euismod", "dictum",
    "Ut", "arcu", "enim", "consectetur", "at", "rhoncus", "at", "porta", "ut", "lacus", "Donec", "nisi", "quam", "faucibus", "tempor",
    "tincidunt", "eu", "porttitor", "id", "ipsum", "Proin", "nec", "neque", "nulla", "Suspendisse", "sapien", "metus", "aliquam", "nec",
    "dapibus", "consequat", "rutrum", "id", "leo", "Donec", "ac", "fermentum", "tortor", "Pellentesque", "nisl", "orci", "tincidunt",
    "at", "iaculis", "vitae", "consequat", "scelerisque", "ante", "Suspendisse", "potenti", "Maecenas", "auctor", "justo", "a", "nibh",
    "sagittis", "facilisis", "Phasellus", "ultrices", "lectus", "a", "nisl", "pretium", "accumsan"
    };
     
    public IEnumerator GetEnumerator()
    {
    return AutoCompletions.GetEnumerator();
    }
    }
    }
  • Remember, not to forget adding the following 2 using directives
    using System.Collections; // for 'IEnumerable'
    using System.Collections.Generic; // for 'List'
  • Thus, we have declared a list of strings as our sample strings. Now to work on the UI.

Making UI and attaching Auto Complete Box with SampleWords class

  • We have to include the namespace inside which we have created the Strings class (SampleWords class according to this example) in the .xaml file in which we need to put AutoComplete box.
  • We will add below line of code in the MainPage.xaml (e.g)
    xmlns:local="clr-namespace:SampleAutoCompleteBox"

The xmlns:local always point to the top level namespace. So, we have defined SampleWords.cs inside the SampleAutoCompleteBox namespace (same as your project name).

  • Create/declare a static resource in the MainPage.xaml file like shown below
<phone:PhoneApplicationPage.Resources>
<local:SampleWords
x:Key="AutoCompletions" />
</phone:PhoneApplicationPage.Resources>

Here, 'local:' will have the class file name in which we have declared the list of stings. We have also defined a key as 'AutoCompletions' so that it can be directly used by it in future.

  • Now we will drag and drop the AutoCompleteBox tool from the Toolkit on to the MainPage design view. By this, the corresponding code will be inserted into the code view.
  • We will assign the value StaticResource AutoCompletions to the property ItemsSource as shown below:
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <toolkit:AutoCompleteBox HorizontalAlignment="Left" Width="450" Grid.Row="0"
    Name="autoCompleteBox1" VerticalAlignment="Top"
    ItemsSource="{StaticResource AutoCompletions}"/>
    </Grid>
  • Now build the project and run it to see AutoCompleteBox tool running.

Source code

  • The example illustrated in the article can be downloaded from here .

Comments

Contents

Chintandave er - sceenshot.

Hi Pooja. A sample screenshot of the UI generated by this code would be give better idea to understand beginner level developers before implementing the whole code.

Nice article though.

Chintan.

Chintandave er 07:43, 25 January 2012 (EET)

Croozeus - Sub-edited

@Pooja, the article looks good. I've subedited the article and here are some comments,

  • I've added an abstract - removed the introduction part, since the abstract (one liner) gives pretty good idea about what's in the article.
  • Created a new section called "Pre-requisites" - moved relevant material from your 'Introduction' to this section.

Couple of tips related to construction of sentences while writing tutorials,

  • If possible, try to avoid having second person singular ("You") as the subject of the sentence. Using plural i.e "We" may be more appropriate or just have generic instructions. Eg. "you will have to download the Silverlight Toolkit" may be read better as "we need to download the Silverlight Toolkit"
  • Try to avoid imperative sentences. For example, "Don't forget to add the following 2 using directives" - may sound commanding. Rather you may use - "Remember, not to forget adding the following 2 using directives."

Looking forward to see more new articles on WP7!

//P

croozeus 09:40, 25 January 2012 (EET)

Croozeus - Source code

The zip archive contains some temp files generated by the build system, which are not required and you may want to remove them. For eg. the files in the Debug folder, etc.

//P

croozeus 10:09, 25 January 2012 (EET)

Pooja 1650 -

Thank you Pankaj & Chintan for your valuable feedback. I will always try to implement your suggestions.

pooja_1650 11:08, 25 January 2012 (EET)

Hamishwillee - Nice job.

From the category page "While we welcome articles about Windows Phone, there are many excellent resources on MSDN and elsewhere. What we need most are articles showing how to port between Windows Phone and other Nokia platforms."

So if you're writing an article on Windows phone please consider:

  • whether you can write a comparative article showing how to do the same thing on Qt
  • don't duplicate content that is on MSDN. The exception is you're doing it "better" than articles you find elsewhere or covering aspects that they missed. Providing a downloadable example when the original doesn't also adds value.
The above is a general point that I'm hoping you will consider in future - I haven't evaluated whether this could be a comparative article or if there is better documentation elsewhere. I can say that this looks to be well written and does "all the right things" - like having images, downloadable examples, and references to the official API reference. Good job - and thanks to moderators and Champions for giving pooja such good advice.

hamishwillee 03:02, 26 January 2012 (EET)

This page was last modified on 26 April 2012, at 04:38.
332 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2012 All rights reserved