views:

255

answers:

3

A simple WPF ListView:

<ListView Name="recordContainer" ItemsSource="{Binding Path=MyCollection}">
   <GridView>
      <GridViewColumn Width="260" Header="Name" DisplayMemberBinding="{Binding Path=Name}"/>
      <GridViewColumn Width="100" Header="Value" DisplayMemberBinding="{Binding Path=Value}"/>
   </GridView>
</ListView>

MyCollection is a property on my Page:

public ObservableCollection<MyData> MyCollection
{
  get
  {
     return myCollection;
  }
}

and this is my data object:

public class MyData
{
    public string Name { get; set; }
    public string Value { get; set; }
}

I fill up myCollection in the contructor of the page (before InitializeComponent) but the list is coming up empty!

I have the exact same configuration on other pages and it works fine - what am I missing?

Any help appreciated!

+1  A: 

Set the DataContext of the page to the page itself :

this.DataContext = this;
Thomas Levesque
thanks it works! not clear though why it works in the other pages where I am not explicitly doing it
JohnIdol
+2  A: 

What Thomas said... or...

What you're missing is that the binding is actually examining the DataContext. You can set the source differently a number of ways, however.

Think of it this way... Where is the binding to look for MyCollection? Binding is just a class; it isn't all knowing. You have to tell it where to look. By default, this is the DataContext. The DataContext is shared among the logical tree elements of your UI, with items lower in the tree able to see DataContexts higher up in the tree, or even override this value for items lower than themselves.

In your case, you want a value located on your Page which is not the DataContext. You must tell Binding how to find this. You can do this via the RelativeSource property.

{Binding MyCollection RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Page}}}

This is a combination of three markup helpers, Binding, RelativeSource, and Type.

RelativeSource finds an object in the tree relative to the current position. In this case, we are Finding an Ancestor (we're looking up the tree). RelativeSource walks up the tree looking for the first object of type Page. Once it finds this, it returns it to Binding. Binding then examines this object for a property MyCollection.

Will
Plus one to you Will.. It just wrong to use this.DataContext = this.. Whats the point of having a viewmodel, if you are going to use your control as viewmodel..
Arcturus
A: 

Hi all!

Here: http://simplesample.site90.com/wpf_binded_listview.php

is a complete example showing a ListView binded to a ObservableCollections and manipulating them using Commands.

Hope this helps.

Rafa