What's the difference between the ItemsSource and DataContext properties

Tags: Silverlight, Technical

When I started silverlight (not long ago) I had a question that really bothered me: What's the difference between the ItemsSource and DataContext property of a list box. When I searched around, I found answers that go along the line of: a DataContext is typically used for...whatever... and ItemsSource is used for ... whatever... but when O read those explanations I still wasn't happy, something was still bothering me, it still wasn't clear to me "when" do I set a "Data"Context" and "when" do I set the ItemSource instead. So as usual, if you didn't get the answer right, you didn't ask the question right.

So here is my attempt to answer this question - while I'm teaching you how to ask the same question differently (like a detective) - you should really ask: When is it that, if I set the DataContext I won't get the same results as setting the ItemSource or vise versa. And as the Talmud states "Within a question from the wise lies half the answer". This will lead you right to the answer…

…If you bind a Person list to the DataContext of your list, your list will still not be populated, just as if you bind the name property of a person object to the DataContext of a textbox it won't display the name.  ItemsSource is to a ListBox as the text property is to a TextBox, It tells the binding system to fill the items with all of the items in that list, which datacontext does't do. A DataContext by itself never displays or generates any UI content.

Now that I explained to you what ItemSource does, you can go back and read all the other posts that try to answer this question by explaining what the DataContext typically does...

1 Comment

  • lerobg said

    <strong><a href="http://www.jewelryoff.com/">wholesale jewelry</a></strong><br>
    <strong><a href="http://www.jewelryoff.com/pandora-jewelry-c-77.html">Pandora</a></strong><br>
    <strong><a href="http://www.jewelryoff.com/pandora-jewelry-c-77.html">Pandora Jewelry</a></strong><br>
    <strong><a href="http://www.jewelryoff.com/pandora-jewelry-c-77.html">cheap Pandora Jewelry</a></strong><br>
    <strong><a href="http://www.jewelryoff.com/pandora-jewelry-c-77.html">buy Pandora Jewelry</a></strong><br>

Add a Comment

You must log on to comment.