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

In this post, you’ll learn about the Win32 Error “0x000019E5 – ERROR_COULD_NOT_RESIZE_LOG” that you get when debugging system erors in...
In this post, you’ll learn about the error “CO_E_DBERROR 0x8004E02B” that is returned when working with COM based APIs or...
In this post, you’ll learn about the Win32 Error “0x000019D0 – ERROR_LOG_BLOCK_VERSION” that you get when debugging system erors in...