XAML中的WPF ListView绑定ItemsSource

时间:2021-09-27 17:02:17

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

我有一个简单的XAML页面,其上有一个像这样定义的ListView

<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

如果我在后面的代码中设置listview的ItemsSource,就像这样

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

但是,如果我删除该行并尝试绑定XAML

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

it no longer works.

它不再有效。

Why doesn't the binding in the XAML work?

为什么XAML中的绑定不起作用?

2 个解决方案

#1


9  

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

如果您还没有这样做,例如在XAML中,您需要为绑定设置DataContext。此外,由于People属性未实现INotifyPropertyChanged,您可能希望在InitializeComponent之前创建此列表,至少在设置DataContext之前,确保在评估绑定时列表已准备就绪。您可以稍后添加到ObservableCollection,但如果您在该点之后创建它而不通知UI它将无法工作

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


4  

Put this line after the existing code in xaml.cs

将此行放在xaml.cs中的现有代码之后

this.DataContext = People;

and replace your xaml with

并用你的xaml替换

ItemsSource="{Binding People}" 

to

ItemsSource="{Binding}"

#1


9  

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

如果您还没有这样做,例如在XAML中,您需要为绑定设置DataContext。此外,由于People属性未实现INotifyPropertyChanged,您可能希望在InitializeComponent之前创建此列表,至少在设置DataContext之前,确保在评估绑定时列表已准备就绪。您可以稍后添加到ObservableCollection,但如果您在该点之后创建它而不通知UI它将无法工作

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


4  

Put this line after the existing code in xaml.cs

将此行放在xaml.cs中的现有代码之后

this.DataContext = People;

and replace your xaml with

并用你的xaml替换

ItemsSource="{Binding People}" 

to

ItemsSource="{Binding}"