March 14, 2012 07:44 by
Scott
In this article let us see how to use a AutoCompleteBox Control in a Silverlight application. As usual, open the visual studio and select the Silverlight project.
First let us drag a AutoCompleteBox to Stack Panel as shown below into MainPage.xaml.
<sdk:AutoCompleteBox x:Name="CountriesNames" Width="200" />
Now we will add List to AutoCompleteBox from MainPage.xaml.cs as shown below. In the below code, First i prepared list of type string and assigned a name "Countries" to it. Then i added strings( Countries names) to the list "Countries".
List<string> Countries = new List<string>();
Countries.Add("India");
Countries.Add("USA");
Countries.Add("Japan");
Countries.Add("UK");
Countries.Add("Australia");
Countries.Add("Switzerland");
CountriesNames.ItemsSource = Countries;
At last i am binding this list " Countries" to the "AutoCompleteBox" using its name "CountriesNames". Thats it!!! Just press F5 and see the result. The output of the above code looks like as
<image>
Note : For the people who find it difficult to integrate the above code, I am pasting the complete code here.
MainPage.Xaml:
<UserControl x:Class="SilverlightTest1.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"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<StackPanel Background="White">
<StackPanel x:Name="LayoutRoot" Orientation="Horizontal">
<TextBlock Text="CountriesList: " Margin="5" VerticalAlignment="Center" />
<sdk:AutoCompleteBox x:Name="CountriesNames" Width="200" />
</StackPanel>
</StackPanel>
</UserControl>
MainPage.Xaml.cs:
public MainPage()
{
InitializeComponent();
List<string> Countries = new List<string>();
Countries.Add("India");
Countries.Add("USA");
Countries.Add("Japan");
Countries.Add("UK");
Countries.Add("Australia");
Countries.Add("Switzerland");
CountriesNames.ItemsSource = Countries;
}