wpf - 多对多(学生、类(class)示例)Datagrid xaml 绑定(bind)

标签 wpf entity-framework xaml data-binding

我有一个 WPF/ Entity Framework 应用程序,
我的模型有学生,类(class)实体
学生和类(class)之间存在多对多的关联/关系(学生可以有很多类(class),类(class)有很多学生)

学生 [ Id ,Name , Classes (Navigation property) ]
类 [ Id ,Title, Students(导航属性 ]

在这种情况下, Entity Framework 不显示连接/关系表。

我有 2 个 DataGirds 作为主要详细信息,学生网格是主要信息,类(class)是详细信息

如何设置绑定(bind)到类网格(详细信息网格),以便我可以向所选学生添加和删除类?

我想要的是从主网格中选择一个学生并添加或删除他的类(class)

这是我的类(class)

<DataGrid x:Name="classesDataGrid"
                      AutoGenerateColumns="False"
                      EnableRowVirtualization="True"
                      Height="200"
                      ItemsSource="{Binding Source={StaticResource studentClassesViewSource}}"
                      RowDetailsVisibilityMode="VisibleWhenSelected"
                      Width="380">
                <DataGrid.Columns>


                    <DataGridComboBoxColumn Header="Class Name From Combo"
                                            Width="*"

                                            ItemsSource="{Binding Source={StaticResource classViewSource}}" 
                                            DisplayMemberPath="Name"

                                            SelectedItemBinding="{Binding Students}" 

                                            />
                    <!--<DataGridTextColumn x:Name="durationColumn"
                                        Binding="{Binding Duration}"
                                        Header="Duration"
                                        Width="SizeToHeader" />-->

                </DataGrid.Columns>

当关系是一对多时,这种情况可以正常工作,但我无法使其适用于多对多关系。

更多关于这篇文章
here

最佳答案

我不确定我是否正确解决了您的问题:
您想知道如何解决 xaml 中的主细节绑定(bind)问题吗?

如果您有一个带有类集合作为导航属性的学生,您可以简单地为学生使用一个网格,例如:

<DataGrid x:Name="masterGrid"
          ItemsSource="{Binding Path=Students}" />

另一个是详细信息(您的类(class)-反之亦然,没关系):
在这种情况下,我建议您使用元素名称绑定(bind)
<DataGrid x:Name="detailsGrid"
          DataContext="{Binding ElementName=masterGrid, Path=SelectedItem}"
          ItemsSource="{Binding Path=Classes}" />

希望有帮助(至少对于最初的点火)......

关于wpf - 多对多(学生、类(class)示例)Datagrid xaml 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17127552/

相关文章:

wpf - 使用 IDataErrorInfo 和更新按钮状态在验证期间启用禁用保存按钮

c# - 如何在 WPF 中保存控件的非模糊图像?

c# - NHibernate vs Entity Framework 6 对大量用户的性能

wpf - 如何将对象 bool 属性绑定(bind)到 CheckBox IsChecked 属性?

c# - 是否有任何计划在未来的 VS 版本中支持 XAML 的编辑和继续?

c# - 如何使用响应式扩展等待 IObservable<T> 完成而不阻塞 UI?

c# - 如何将App.XAML样式应用到当前窗口

c# - 在 Entity Framework 对象中使用外部对象

c# - 在不破坏已发布站点的情况下,代码优先 EF 中更新数据库模型的工作流程是什么?

c# - 将属性绑定(bind)到方法