tags:

views:

67

answers:

2

Hi,

I'm attempting to learn WPF by unravelling a frankly nightmarish project written by the guy who was in this job before me. Sorry if some of my questions are pretty much homework-level but I'm trying to work out what existing XAML does, with an insufficient understanding of the concepts behind it...

Anyway, I have a ListView with this as part of its definition:

<ListView 
    DataContext="{StaticResource XMLFileGroups}"
    ItemContainerStyle="{StaticResource XMLItemStyle}"
    ItemsSource="{Binding}">

Now, I can kind of get my head around what the "DataContext" and "ItemContainerStyle" lines are doing; they appear to be referencing a method of sorting an existing list, and a structure defining some visual behaviour of the ListView, respectively.

What's wrecking me is the fact that the ItemsSource is listed as "{Binding}". All that says to me is that there is some kind of databinding in place, but I don't understand how the line can possibly be meaningful and yet removing it stops any data from being displayed.

Can someone shed some light on what is happening here, or where I should look for the actual binding definition? I just don't understand what I'm seeing, here.

+11  A: 

Without a path, {Binding} will bind to the DataContext itself.
Adding a path will bind to a property of the datacontext.

SLaks
So "{Binding}" specifically links back to the collection being created by the DataContext property of the ListView? Is this something that generally happens for View-type objects or is it specific to ListView? Apologies for begging for more information, but I'm sure you can appreciate that searching for 'ItemsSource="{Binding}"' isn't a narrow enough search for me to be getting many results, hence my asking this question in the first place...
Frosty840
I'm not sure what you're asking. `{Binding}` can be used in any context where the value you want to bind to is the `DataContext` itself. It's most frequently used to bind a DataSource.
SLaks
I thought that was the case, but I couldn't really be sure. Thanks for the clarification.
Frosty840
+1  A: 

That example specifies that the binding is the DataContext. The same thing in the code behind would be

MyList.ItemsSource = new Binding();

You can also do stuff like:

ItemsSource="{Binding YourBindingField, Source={StaticResource YourStaticDataSource}}"

which would translate to this in code behind:

        MyList.ItemsSource = new Binding() {ElementName = "YourBindingField", Source = YourStaticDataSource};

Hope that helps

danijels
Sorry, I failed to mention that what I'm working on is (in theory) an MVVM project, so there isn't any code-behind. While I'm sure what you say is correct, I don't have any context in which to interpret it.
Frosty840