在WPF中将DataGrid绑定到通用列表的最佳方法

8

我正在尝试在WPF中将DataGrid绑定到一个泛型列表。

以下代码会导致每行数据都显示为空白行(即如果有5行,则显示5行,但单元格中不显示任何数据):

List<DataRow>  DataBindingSource = GuestList.Where(row =>
  (row.Field<long>("FK_GROUP_ID") == Int64.Parse(cmbGroup.SelectedValue.ToString())) &&
  (row.Field<long>("FK_AGE_GROUP_ID") != (int)L_Age_Group.Child))
  .ToList();

gvwAddultDetails.ItemsSource = DataBindingSource;

如果我将我的对象列表转换为DataTable,它可以工作(显示数据)。例如:
List<DataRow> DataBindingSource = GuestList.Where(row =>
  (row.Field<long>("FK_GROUP_ID") == Int64.Parse(cmbGroup.SelectedValue.ToString())) &&
  (row.Field<long>("FK_AGE_GROUP_ID") != (int)L_Age_Group.Child))
  .ToList();

gvwAdultDetails.ItemsSource = DataBindingSource.CopyToDataTable().DefaultView;

但如果我有一个List<DataRow>,我该如何将它转换为DataTable

在WPF中将DataGrid绑定到List的最佳实践是什么?

1个回答

7

在WPF中将DataGrid绑定到泛型列表的一种方法:

MainWindow.xaml.cs

Grid.DataContext = DataBindingSource;

MainWindow.xaml

<DataGrid
  AutoGenerateColumns="True"
  ItemsSource="{Binding}"
  Name="Grid" />

但是它可能无法与 DataRow 一起使用,因为在 WPF 中,如果要将其绑定到某些东西,则需要它是一个属性。

- 其他想法 -

以下是如何将通用列表转换为 DataTable

如何在 WPF 中将 DataGrid 绑定到通用列表:

特别地(在 WPF 中非常好的功能):


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接