Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF ListView Databind not binding!

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!

like image 879
JohnIdol Avatar asked May 14 '26 04:05

JohnIdol


1 Answers

Set the DataContext of the page to the page itself :

this.DataContext = this;
like image 152
Thomas Levesque Avatar answered May 16 '26 20:05

Thomas Levesque