Showing posts with label ListBox. Show all posts
Showing posts with label ListBox. Show all posts

Monday, March 30, 2009

Data Binding of ListBox in Silverlight

In this post I'll show how to use data binding with the listbox in silverlight. Suppose that you have a class Person which contains ID, Name, Address like this:
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
Now i have a List lstPerson. And i want to show Name in the ListBox.
So in the .xaml I define a listbox as:
<ListBox x:Name="ListBoxPerson">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ForeignKeyName}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

And now in .xaml.cs i define the itemSource of the ListBox as ListBoxPerson.ItemSource=lstPerson;
So, this is so simple to use data binding in ListBox.