views:

120

answers:

3

I've set up my DataContext like this:

<Window.DataContext>
    <c:DownloadManager />
</Window.DataContext>

Where DownloadManager is Enumerable<DownloadItem>. Then I set my DataGrid like this:

<DataGrid Name="dataGrid1" ItemsSource="{Binding Path=/}" ...

So that it should list all the DownloadItems, right? So I should be able to set my columns like:

<DataGridTextColumn Binding="{Binding Path=Uri, Mode=OneWay}"

Where Uri is a property of the DownloadItem. But it doesn't seem to like this. In the visual property editor, it doesn't recognize Uri is a valid property, so I'm guessing I'm doing something wrong.

It was working before, when I had the data grid binding to Values, but then I took that enumerable out of the DownloadManager and made itself enumerable. How do I fix this?

PS: By "doesn't work" I mean it doesn't list any items. I've added some to the constructor of the DM, so it shouldn't be empty.

+6  A: 

Try ItemsSource="{Binding}". It should be enough.

Danko Durbić
Brilliant! Worked like a charm.
Mark
Or `ItemsSource="{Binding Path=.}"`. Both do the same job.
Veer
A: 

As an alternative sol'n to Danko's answer, I discovered Static Resources! (I'm a WPF noob)

<Window x:Class="ImageGetGUI.MainWindow"
    ...
    <Window.Resources>
        <c:DownloadManager x:Key="dm"/>
    </Window.Resources>
    ...
    <DataGrid Name="dataGrid1" ItemsSource="{StaticResource dm}" ...
Mark
You can also name your Window.DataContext, i.e., `<Window.DataContext> <c:DownloadManager x:Name="dm" /> </Window.DataContext>` This will allow you to access the DataContext by name in your code behind if needed. You may also want to take a look at an ObjectDataProvider; I've found them immensely helpful when placed in the application's resource dictionary. For example, when you have a collection that is re-used on multiple views, i.e., a `List<US States>`.
Metro Smurf
+2  A: 

In response to the OP's question of why {Binding} works, but {Binding Path=/} does not work, I'm adding the following info as an answer to clarify the difference.

The following is taken from MSDN Data Binding Overview > Binding to Collections:

Current Item Pointers

Views also support the notion of a current item. You can navigate through the objects in a collection view. As you navigate, you are moving an item pointer that allows you to retrieve the object that exists at that particular location in the collection. For an example, see How to: Navigate Through the Objects in a Data CollectionView.

Because WPF binds to a collection only by using a view (either a view you specify, or the collection's default view), all bindings to collections have a current item pointer. When binding to a view, the slash ("/") character in a Path value designates the current item of the view. In the following example, the data context is a collection view. The first line binds to the collection. The second line binds to the current item in the collection. The third line binds to the Description property of the current item in the collection.

<Button Content="{Binding }" />
<Button Content="{Binding Path=/}" />
<Button Content="{Binding Path=/Description}" />
Metro Smurf
Ohhh..... okay. Now I get it. Current Item != DataContext. It's the first item *in* the DataContext.
Mark