我有一个 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/