Silverlight – Databinding ListBox
A couple of weeks ago I introduced the concept of databinding in Silverlight. If you missed it you might want to go take a look because this post assumes you know the basics already.
You should remember that databinding in Silverlight has very little to do with databases and everything to do with data. We’ve already looked at how to bind a basic property to an attribute in our XAML, but what if our XAML represents a list? How do we bind to it?
It’s actually pretty nearly the same as last week. Let’s start with our basic class again:
namespace SilverlightApplication1 { public class Person { public string First { get; set; } public string Last { get; set; } private List<string> m_sample = new List<string>(); public List<string> Sample { get { if(m_sample.Count == 0) { m_sample.Add("Thing One"); m_sample.Add("Thing Two"); m_sample.Add("Thing Three"); } return m_sample; } } } }
This is the same code we used from the previous Silverlight databinding example except we’ve added a property named Sample that is a List of strings. We’ve hard-wired the list to return some content for us.
Next we want to get this wired to a list box in our XAML. This is what our list box code looks like:
<ListBox Height="224" x:Name="m_listSample" Width="353" ItemsSource="{Binding Mode=OneWay, Path=Sample}"/>
Note that we are binding to ItemSource which expects an IEnumerable property.
When we run the code we get this:

Now the really cool thing about all this is that once you have your class built, you can actually do all your DataBinding from Expression Blend. To do this, select the item you want to bind. In the properties you can scroll to “Common Properties” where you will see the same ItemsSource property databound to above. To the right of it, to the right of all of the properties, is a small square box. If you click it, you’ll get a list of options. One of those is “Databinding…” You can use that to set the DataContext as well as the property the Control property should be bound to.
It might take some experimenting to get the hang of it, but this is a much easier way to bind than trying to remember the XAML syntax.
Other Related Items:
3 Volt CR-2450 Battery for Some Dive Computers - See Description for Detailed ListYou just set up your equipment for the first dive of your vacation in Saint Somewhere, as you press the activation button on your dive computer it goe... Read More >
Unlisted Women's Guest List Quarter RuffleYou'll be on the A-list wearing the Unlisted Guest List sandals. Satin upper in a dress sandal style with ruffled accents and a heel sling strap featuring an adjustable buckle. Smooth lining and cushioned footbed. Wrapped midsole and matching wrapped 3 1/4 inch high heel. Flat traction outsole.
LEGO Star Wars Rebel Trooper Battle Pack (8083) Includes 3 rebel troopers and 1 pilot! Awesome Hoth speeder! Awesome blasters!Bring home a whole army on Hoth! These awesome figures comed with sp... Read More >










[...] Silverlight – Databinding ListBox (Dave M. Bush) [...]