views:

100

answers:

3
+3  A: 

Think of "DataContext" as the default value for "Source" in a binding.

When you create a binding, you can specify the path and source, like this (I'll use TextBox as an example):

<TextBox Text="{Binding Path=Foo,Source={StaticResource Bar}}" />

So my TextBox.Text property is bound to a Foo property on an object called Bar (a resource somewhere in the application).

However, if you have a whole bunch of things that you want to bind to properties on Bar, it's easier to set Bar as the DataContext of the parent container. A Binding without a Source will just use the DataContext by default, and DataContext flows through to child controls from the parent. So:

<StackPanel DataContext="{StaticResource Bar}">
    <TextBox Text="{Binding Path=Foo}" />
    <TextBox Text="{Binding Path=Fizz}" />
    <TextBox Text="{Binding Path=Buzz}" />
</StackPanel>

All of the TextBoxes are still binding to properties on Bar, but they're doing it without setting it as a Source explicitly.

So let's have another look at the dialog you posted. It's giving you several options for the "source" of the ItemsSource binding. When you choose "DataContext", you're telling Visual Studio that the ItemsControl doesn't need to know the source - it'll pick it up from the DataContext of the parent container (maybe even the Window itself).

If you chose one of the other options (ElementName, RelativeSource or StaticResource) then you'd be setting the binding's source explicitly for that ItemsControl.

Once you've told it that it's binding to the DataContext, you'll need to drop into the "Path" section of the dialog and tell it which property to bind the items of the control to. In the end, the markup would look something like this (assuming it's a ListBox):

<ListBox ItemsSource="{Binding Path=Foos}" />

So the items in the ListBox are coming from a property called "Foos", and that property is on an object that's set in the DataContext somewhere higher in the logical tree (perhaps on the Window itself).

Matt Hamilton
What I really don't get is when I choose `DataContext` as the source, then dropdown to `Source`, there are no options. I only see "None". Specifically where in my C# code am I supposed to put something like `List<MyClass> items` so that it will appear in the ItemsSource dialog?
Mark
The point is that if you tell it you want to use the DataContext, you're saying you don't want to set the Source. It doesn't make sense to have both. Use DataContext and Path.
Matt Hamilton
In a class you have a property with your items list. It could be any class, this is the idea that makes MVVM work. You then set that class as DataContext. Now you can set the property as path.
Daniel Rose
+1  A: 

You rarely need to use the data context of a control outside of the control. The most common use case for setting DataContext(DataContext = this;) is within UserControl's code-behind to make all controls within the UserControl to bind to the control's properties.

When you use a ListBox, setting ItemsSource is sufficient, unless you are doing something funky.

Igor Zevaka
A: 

This is a pretty good walkthrough: http://windowsclient.net/learn/video.aspx?v=315275

Specifically, you need to set the DataContext first to tell it where to look for the ItemsSource. The easiest way is to set this on the Window through the XAML:

<Window.DataContext>
    <controllers:DownloadManager />
</Window.DataContext>
Mark