WPF ListView Databind is not tied!

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 populate myCollection in the page constructor (before InitializeComponent), but the list is empty !

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

Any help appreciated!

+3
source share
4 answers

Set the DataContext of the page to the page itself:

this.DataContext = this;
+4
source

What Thomas said ... or ...

What you are missing is that the binding is really looking at the DataContext. However, you can set the source differently in several ways.

... MyCollection? - ; . , . DataContext. DataContext , , DataContexts , , .

, , DataContext. , . RelativeSource.

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

, Binding, RelativeSource Type.

RelativeSource . ( ). RelativeSource , . , . , MyCollection.

+4

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

- , ListView, ObservableCollections, .

, .

+2

, , listview gridview. , listview .

     ListView = Layout Coordinator

         GridView = Layout Style

               Elements = Items to be displayed

, , ListBox.

(p.s. )

0

Source: https://habr.com/ru/post/1715431/


All Articles