.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Silverlight >> Post New Resource Bookmark and Share   

 Subscribe to Articles

AutoCompleteBox in Silverlight 4.0

Posted By:Dhananjay Kumar       Posted Date: March 03, 2011    Points: 200    Category: Silverlight    URL: http://www.dotnetspark.com  

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

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

It works like below,



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.



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



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


     
  2. Set the Binding, ValueMemberPath and FilterMode.


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


Set the DataContext

We need to set the DataContext of AutoCompleteBox in code behind



For reference,

MainPage.Xaml



    
        
            
                
                    
                
            
            
        
     

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 GetCountry()
        {
            List lstCountry = new List();
            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.

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend