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 .

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

One Response

  1. Norton Stanley May 27, 2014 Reply

Leave a Reply