【问题标题】:Bind two related DataSets to two DataGrids将两个相关的 DataSet 绑定到两个 DataGrid
【发布时间】:2012-06-07 20:44:56
【问题描述】:
我在绑定两个相关表的 DataSet 时遇到问题,我已经搜索了很多次。我不确定如何使用 WPF DataGrid 执行此操作(它似乎与使用 WinForms DataGrid 不同)。
我正在使用 MVVM 模式并将我的 DataContext 与我的 DataSet 关联,一个网格在 ItemsSourceProperty 上绑定到其中一张表。我想知道您如何在dataGrid1 中选择一项并将相关行自动绑定到dataGrid2。
【问题讨论】:
标签:
c#
wpf
mvvm
datagrid
wpfdatagrid
【解决方案1】:
您只需要知道您的关系名称。让我们假设表是 Student 和 Classes,关系名称是:FK_Student_Classes,那么您的绑定如下所示:
<DataGrid x:Name="grdStudents" ItemsSource="{Binding MyDataSet.Student}" AutoGenerateColumns="True" Grid.Row="0"/>
<DataGrid ItemsSource="{Binding ElementName=grdStudents, Path=SelectedItem.FK_Student_Classes}" Grid.Row="1"/>
当您在学生网格中选择一行时,您将在班级网格中看到所有相关行。