AutoCompleteBox in Silverlight 4.0

No.of Views1064
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  22 Mar 2011 09:03:02
Tag : Silver Light and XAML , List Controls
In this article, i will explain AutoCompleteBox control is part of Silverlight 4.0 SDK. It allows us to have a Google style text box.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

AutoCompleteBox control is part of Silverlight 4.0 SDK.  It allows us to have a Google style text box.

It works like below,

Image Loading

AutoCompleteBox will look for suggestion in IEnumerable list.

Create a DataSource to find the suggestion

Very first let us create a Country Class.  This class will serve as Entity class.

 

Image Loading

Create a function and this function will return list of country,

Image Loading

We will set above function as DataContext() of AutoCompleteBox.

Drag and Drop AutoCompleteBox on XAML

1.    From the tool box select AutoCompleteBox and drop on the XAML page

Image Loading

2.    Set the Binding, ValueMemberPath and FilterMode.

Image Loading

3.    Set the DataTemplate and bind the TextBlock where user will type the text.

Image Loading

Set the DataContext

We need to set the DataContext of AutoCompleteBox in code behind

Image Loading

For reference,
MainPage.Xaml

<UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="SilverlightApplication4.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
 
    <Grid x:Name="LayoutRoot" Background="White">
        <sdk:AutoCompleteBox x:Name="atcTextBox" ItemsSource="{Binding}"
                             ValueMemberPath="CountryName" 
                             FilterMode="Contains" 
                             IsTextCompletionEnabled="True"                             
                             Height="30" Margin="62,22,54,248">
            <sdk:AutoCompleteBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding CountryName}" />
                </DataTemplate>
            </sdk:AutoCompleteBox.ItemTemplate>
            
        </sdk:AutoCompleteBox>
 
    </Grid>
</UserControl>

MainPage.Xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
 
namespace SilverlightApplication4
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            atcTextBox.DataContext = GetCountry();
 
        }
 
        List<Country> GetCountry()
        {
            List<Country> lstCountry = new List<Country>();
            lstCountry.Add(new Country { CountryName = "India" });
            lstCountry.Add(new Country { CountryName = "USA" });
            lstCountry.Add(new Country { CountryName = "Australia" });
            lstCountry.Add(new Country { CountryName = "Germany" });
            lstCountry.Add(new Country { CountryName = "England" });
            lstCountry.Add(new Country { CountryName = "Brazil" });
            lstCountry.Add(new Country { CountryName = "China" });
            lstCountry.Add(new Country { CountryName = "Japan" });
            lstCountry.Add(new Country { CountryName = "Denmark" });
            lstCountry.Add(new Country { CountryName = "France" }); 
            lstCountry.Add(new Country { CountryName = "Germany" });
            return lstCountry; 
        }
 
        
    }
}

We can set the source from WCF service, Database, cloud anywhere.

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top