问题
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 = "john@doe-family.com" });
this.People.Add(new Person() { Name = "Jane Doe", Age = 39, Mail = "jane@doe-family.com" });
this.People.Add(new Person() { Name = "Sammy Doe", Age = 7, Mail = "sammy.doe@gmail.com" });
}
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?
回答1:
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 = "john@doe-family.com" });
this.People.Add(new Person() { Name = "Jane Doe", Age = 39, Mail = "jane@doe-family.com" });
this.People.Add(new Person() { Name = "Sammy Doe", Age = 7, Mail = "sammy.doe@gmail.com" });
}
回答2:
Put this line after the existing code in xaml.cs
this.DataContext = People;
and replace your xaml with
ItemsSource="{Binding People}"
to
ItemsSource="{Binding}"
来源:https://stackoverflow.com/questions/26353919/wpf-listview-binding-itemssource-in-xaml