DataBinding Custom Objects to AutoCompleteBox in Windows Phone

In one of my previous blog post , I shared a sample sourecode that binds List of String as ItemSOurce for the AutoCompleteBox .

In this Blog post , i will share the sample sourcecode on how to bind an object to the ItemSource property of the AutoCompleteBox in Windows Phone .

DataBinding Custom Objects to AutoCompleteBox in Windows Phone

Just set the ValueMemberBinding property of the AutoCompleteBox to the Property of the Class like below

ValueMemberBinding=”{Binding Actor}”

where Actor is the Property of the Class “Movie”

public class Movie

{

public string Name { get; set; }

public string Actor { get; set; }

}

Also , create a Text Block inside the Item Template of the AutoCompleteBox and Set its Text property to Bind to the Actor Property

The XAML code of the AutoCompleteBox will look like this

<toolkit:AutoCompleteBox FilterMode="StartsWith" Name="autoCompleteBox1" ValueMemberBinding="{Binding Actor}" Margin="0,0,6,561">

<toolkit:AutoCompleteBox.ItemTemplate>

<DataTemplate>

<StackPanel>

<TextBlock Text="{Binding Actor}" />

</StackPanel>

</DataTemplate>

</toolkit:AutoCompleteBox.ItemTemplate>

</toolkit:AutoCompleteBox>

Now , set the ItemsSource property of the AutoCompleteBox with the List of Movie .

List<Movie> Lst = new List<Movie>();

Lst.Add(new Movie { Name = "Velayutham", Actor = "Vijay" });

Lst.Add(new Movie { Name = "Nanban", Actor = "Vijay" });

Lst.Add(new Movie { Name = "Yohan", Actor = "Vijay" });

Lst.Add(new Movie { Name = "Billa 2", Actor = "Ajith" });

Lst.Add(new Movie { Name = "Maatran", Actor = "Surya" });

autoCompleteBox1.ItemsSource = Lst;

Run the Windows Phone App , you should see the actor names in the suggestions as and when you type the text in the AutoCompleteBox

    1 Comment

  1. May 27, 2014
    Reply

    Nice post… Helpful. Thank you.!

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

This blog post will guide you through several effective methods to troubleshoot and resolve the issue of Microsoft Edge not...
Windows 11 offers a range of audio enhancements that can enrich your listening experience. These enhancements include features like virtual...
Windows 11 brings a fresh and visually stunning design to your desktop, and one of the standout features is the...