How to use AutoCompleteBox in Silverlight 5.0 application?


Want to learn how to use AutoComplete box in Silverlight 5.0? I have here tried to demonstrate the usage of the AutoComplete box. I hope this article will be helpful to all beginners of Silverlight 5.

Autocompletebox saves typing and increases efficiency of the application. It can be used for entering various country names, subject names, country codes, or any standard inputs required to enter.
Concept- Most of the time we put first alphabet of the string in textbox and automatically related strings appear below the textbox which help us to complete the input in lesser time. This functionality can be achieved with this code.
Steps-
1 Create silverlight 5.0 application using Microsoft Visual Web Developer 2010 Express edition and put following code in Main.xaml


<UserControl x:Class="SilverlightApplication2.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" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Grid x:Name="LayoutRoot" Background="White">
<sdk:AutoCompleteBox Height="28" HorizontalAlignment="Left" Margin="64,30,0,0" Name="autoCompleteBox1" VerticalAlignment="Top" Width="120" ValueMemberPath="Technology">
<sdk:AutoCompleteBox.ItemTemplate >
<DataTemplate >
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding Technology}"> </TextBlock>
</StackPanel>
</DataTemplate>
</sdk:AutoCompleteBox.ItemTemplate>
</sdk:AutoCompleteBox>
</Grid>
</UserControl>

2
Create class file Subjects.cs and add properties SubjectFirstAlphabet as well as Technology with get and set methods.
Now put following code in Main.xaml.cs file and your application will be ready to execute.

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 SilverlightApplication2
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
List<Subjects> lst = new List<Subjects>()
{
new Subjects { SubjectFirstAlphabet ="J" , Technology ="Java" },
new Subjects { SubjectFirstAlphabet ="C" , Technology ="C#" },
new Subjects { SubjectFirstAlphabet ="A" , Technology ="ASP" },
new Subjects { SubjectFirstAlphabet ="O" , Technology ="Oracle"}
};
autoCompleteBox1.ItemsSource = lst;
}
}
}

3 Run the application to check the output.
If you enter J then automatically Java will appear below the textbox. Similarly enter C , A , O to get outputs as C# , ASP, Oracle etc.
You can also bind your autocompletebox with data which can be retrieved from database hence dynamic.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: