Home » none » Silverlight – Databinding ListBox

Silverlight – Databinding ListBox

misc_vol4_063 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:

image

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.

Like this Article? Subscribe to get every article sent to your email.

Related Post

  • Silverlight – DatabindingSilverlight – Databinding When I was learning DataBinding in both .NET 1.0 and .NET 2.0, I quickly discovered that most of what I learned about DataBinding for ASP.NET was useless as I moved to Windows Forms and […]
  • Silverlight – Binding ResourceDictionariesSilverlight – Binding ResourceDictionaries In the last two Silverlight posts I've introduced databinding in Silverlight.  We've seen that databinding, while similar to what we've experienced in Windows Forms and ASP.NET […]
  • Silverlight – Navigating DataSilverlight – Navigating Data Last week I demonstrated how to access data from a web service in Silverlight and display it on the page.  Today we are going to continue on with that demo and look at how to […]
  • Silverlight, Web Services and DatasetsSilverlight, Web Services and Datasets I sat down today to learn about using Silverlight and Dataset from a Web Service.  Something you’d think would be rather trivial. I mean, seriously folks.  We use Datasets as a […]
  • Silverlight – Databinding to a Web ServiceSilverlight – Databinding to a Web Service In the last several posts, I’ve demonstrated various ways of getting data onto the screen.  What I’ve purposely omitted up until this point is how to update this information. So […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer. His commitment to quality through test driven development, vast knowledge of C#, HTML, CSS and JavaScript as well as his ability to mentor younger programmers and his passion for Agile/Scrum as defined by the Agile Manifesto and the Scrum Alliance will certainly be an asset to your organization.

One Pingback/Trackback