我试图将DataGrid
绑定到WPF中的通用列表。
以下代码导致列表中每行数据的空行(即如果我有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
?
将DataGrid
绑定到WPF中的“List”的最佳做法是什么?
将DataGrid
绑定到WPF中的通用列表的一种方法:
MainWindow.xaml.cs
Grid.DataContext = DataBindingSource;
MainWindow.xaml
<DataGrid
AutoGenerateColumns="True"
ItemsSource="{Binding}"
Name="Grid" />
但它可能不适用于DataRow
,因为在WPF中,如果你想绑定某些东西,它需要是一个属性。
- 其他想法 -
以下是如何将通用列表转换为DataTable
:
如何将DataGrid
绑定到WPF中的泛型列表:
特别是(WPF中的一个很棒的功能):