Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF ListView Binding ItemsSource in XAML

Tags:

I have a simple XAML page with a ListView on it defined like this

<ListView Margin="10" Name="lvUsers" ItemsSource="{Binding People}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
            <GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}" />
            <GridViewColumn Header="Mail" Width="150" DisplayMemberBinding="{Binding Mail}" />
        </GridView>
    </ListView.View>
</ListView> 

In the code behind I do:-

public ObservableCollection<Person> People { get; set; }

public ListView()
{
    InitializeComponent();

    this.People = new ObservableCollection<Person>();
    this.People.Add(new Person() { Name = "John Doe", Age = 42, Mail = "[email protected]" });
    this.People.Add(new Person() { Name = "Jane Doe", Age = 39, Mail = "[email protected]" });
    this.People.Add(new Person() { Name = "Sammy Doe", Age = 7, Mail = "[email protected]" }); 

}   

If I set the ItemsSource of my listview in the code behind like this

lvUsers.ItemsSource = this.People;

it works and my grid is displayed as expected

However if I remove that line and try and bind in the XAML

<ListView Margin="10" Name="lvUsers" ItemsSource="{Binding People}">

it no longer works.

Why doesn't the binding in the XAML work?

like image 693
David Avatar asked Oct 14 '14 06:10

David


1 Answers

If you don't do it already, in XAML for example, you need to set DataContext for your binding. Also since People property does not implement INotifyPropertyChanged you might want to create this list before InitializeComponent, at very least before you set DataContext, to be sure list is ready when binding is evaluated. You can add to your ObservableCollection later but if you create it after that point without notifying UI it won't work

public ListView()
{
    this.People = new ObservableCollection<Person>();
    InitializeComponent();
    this.DataContext = this;

    this.People.Add(new Person() { Name = "John Doe", Age = 42, Mail = "[email protected]" });
    this.People.Add(new Person() { Name = "Jane Doe", Age = 39, Mail = "[email protected]" });
    this.People.Add(new Person() { Name = "Sammy Doe", Age = 7, Mail = "[email protected]" }); 
}
like image 134
dkozl Avatar answered Sep 17 '22 22:09

dkozl