尝试代替:
listView.ItemsSource = (from person in People select person).ToList();
[DataContext设置控件及其子级的绑定上下文。itemssource设置用于生成控件中项内容的集合。]
您也可以简单地:
listView.ItemsSource = People;
更充分的例子:
主窗口.xaml:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListView x:Name="listView">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn DisplayMemberBinding="{Binding Age}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
主窗口.xaml.cs:
using System.Windows;
namespace WpfApplication2
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var people = new[] { new { Name = "John", Age = 40 }, new { Name = "Bill", Age = 50 } };
listView.ItemsSource = people;
}
}
}