我有一个 WPF DataGrid
,它绑定了List<Person> people
。
public class Person
{
public string Name{get;set;}
public string LastName{get;set;}
public string Address{get;set;}
public int Age{get;set;}
}
public void ShowPeople()
{
myDataGrid.ItemsSource = people;
}
一切都显示得很好,但我想在 DataGrid
中的 TextBox
中显示 Address
。
我将 XAML 代码更改为以下内容:
<DataGrid x:Name="myDataGrid">
<DataGridTemplateColumn Header="Address">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Address}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid>
这个不起作用。它给了我一个错误信息。
在使用 ItemsSource 之前,项集合必须为空。
请帮忙解决。 谢谢。