【问题标题】: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】:

您只需要知道您的关系名称。让我们假设表是 StudentClasses,关系名称是: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"/>

当您在学生网格中选择一行时,您将在班级网格中看到所有相关行。

【讨论】:

  • 这真的是我想要做的!谢谢,我去试试就知道了
猜你喜欢
  • 2013-02-11
  • 2015-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多