SIGN UP MEMBER LOGIN:    
ARTICLE

Customize AutoCompleteBox in WPF

Posted by Diptimaya Patra Articles | WPF with C# July 28, 2010
In this article we will see how we can customize an AutoCompleteBox in WPF.
Reader Level:
Download Files:
 

Introduction

In this article we will see how we can customize an AutoCompleteBox in WPF.

1.gif
 
Creating a WPF Project

Fire up Visual Studio 2010, create a WPF Application, and name it as AutoCompleteSample2.

2.gif

Sometimes we need to display more than one item in the auto complete box suggestion. For example state code with state names.

So we got two columns in the suggestion. 

I was trying hard to customize it in Expression Blend, but turns out it was very simple.

Let's have a class of Codes with the following properties.

3.gif 

Now let's create a list of Codes and bind that to the AutoCompleteBox.

4.gif 

Now without customization, let's run the application and see what we are getting as output.

5.gif 

So we are getting the Class name instead.

Now we can simply customize the Item Template of the AutoCompleteBox as follows:

6.gif 

As you see in the above code display, I have two textboxes which would bind to the respective properties of the ItemsSource.

Now let's run the application again and see the output.

7.gif 

In the above figure, I have typed "a" but still it's displaying all the states.

To achieve that we need to have the property called ValueMemberPath.

Set the value as the property you need to bind, in our case Name.

8.gif
 
Now finally we can run the application for desired output.

9.gif 

Hope this article helps.

Login to add your contents and source code to this article
share this article :
post comment
 

hirak, you need the namespace declaration for acb: xmlns:acb="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"

Posted by Drew Sheneman Feb 16, 2011

Hi, I followed the same code. But it does not work. It asks for acb which you have mentioned in the XAML file. Let me know, how to make it working. Thanks, Hirak

Posted by hirak sengupta Feb 16, 2011

Would there be any way to loop through the items in the suggest list and apply a different background color to the rows? Thanks!

Posted by Drew Sheneman Dec 29, 2010

je te remercie c très intérssant

Posted by nesrine meliene Dec 02, 2010

Hey Bro,


Thanks for the post...It really helped...Was working with a usercontrol with a grid to select items...now it was so easy using this sample...

Any body who wants to load the same from dataset..

List<Codes> codesList = new List<Codes>();

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    codesList.Add(new Codes { item = ds.Tables[0].Rows[i]["StockCode"].ToString(), description = ds.Tables[0].Rows[i]["Description"].ToString() });
                }
                
                
                autoCompleteBox1.ItemsSource = codesList;

Posted by mohsin aboobacker Jul 29, 2010
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor