c# - 如何在单个datagridview wpf linq中绑定四个表
问题描述
我有单个datagridview,我想将它与相互连接的四个表绑定,照片解释了 四个表的关系
我试图将它与 wpf 绑定,但我的 loadData 代码 无法为:
CollegeContext cd = new CollegeContext();
var query = (from sc in cd.StudentStatments
join l in cd.Levels on sc.IdLevel equals l.Id
join b in cd.Branches on sc.IdBranch equals b.Id
join su in cd.Subjects on sc.IdSubject equals su.Id
select new DGItem
{
NameSub = su.Name,
TypeOfBranch = b.TypeOfBranch,
Code = su.Code,
NameLev=l.Name,
NumOfStudent = sc.NumberOfStudent
}).ToList();
studentStatmentsDataGrid.ItemsSource = query;
我的xaml是:
<DataGrid x:Name="studentStatmentsDataGrid" AutoGenerateColumns="False" FlowDirection="RightToLeft" EnableRowVirtualization="True" ItemsSource="{Binding }" Margin="128,284,0,0" RowDetailsVisibilityMode="VisibleWhenSelected">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Subject.Name}" Header="NameSubjects" Width="SizeToHeader"/>
<DataGridTextColumn Binding="{Binding TypeOfBranch}" Header=" Branch" Width="SizeToHeader"/>
<DataGridTextColumn Binding="{Binding Code}" Header=" code" Width="SizeToHeader"/>
<DataGridTextColumn Binding="{Binding Levels.Name}" Header=" Level" Width="SizeToHeader"/>
<DataGridTextColumn Binding="{Binding NumberOfStudent}" Header="NumberOfStudent" Width="SizeToHeader"/>
</DataGrid.Columns>
</DataGrid>
和我的用户控制资源:
<UserControl.Resources>
<Color x:Key="1">#FF3580BF</Color>
<Color x:Key="Color1">#FFECE3E3</Color>
<Color x:Key="Color2">#FF3580BF</Color>
<Astmara6:Branches x:Key="branches"/>
<CollectionViewSource x:Key="branchesViewSource" Source="{Binding _Branches, Source={StaticResource branches}}"/>
<Astmara6:Section x:Key="section"/>
<CollectionViewSource x:Key="sectionsViewSource" Source="{Binding Sections, Source={StaticResource section}}"/>
<Astmara6:Levels x:Key="levels"/>
<CollectionViewSource x:Key="levelsViewSource" Source="{Binding _Levels, Source={StaticResource levels}}"/>
<Astmara6:Subjects x:Key="subjects"/>
<CollectionViewSource x:Key="subjectsViewSource" Source="{Binding _Subjects, Source={StaticResource subjects}}"/>
<Astmara6:TeachSubBranch x:Key="teachSubBranch"/>
<CollectionViewSource x:Key="subjectsViewSource1" Source="{Binding Subjects, Source={StaticResource teachSubBranch}}"/>
<Astmara6:StudentStatment x:Key="studentStatment"/>
<CollectionViewSource x:Key="studentStatmentsViewSource" Source="{Binding StudentStatments, Source={StaticResource studentStatment}}"/>
</UserControl.Resources>
我希望在我的 datagridview 中显示 su.Name , b.TypeOfBranch ,su.Code ,l.Name, sc.NumberOfStudent 但它没有显示所有数据
解决方案
通过设置 ItemsSource={Binding},您将其绑定到 DataGrid.DataContext。当您在代码中设置值时,此绑定仍然处于活动状态 - 因此 ItemsSource 始终指向 DataContext,我假设它是空的。
您有两个选择:要么从 xaml 中的 DataGrid 中删除 ItemsSource 绑定,要么在后面的代码中设置 DataContex 而不是 ItemsSource。
推荐阅读
- excel - 如何使用公式在excel中创建数字模式
- r - 如何在R中查找列表是否包含空值?
- spring-boot - Docker swarm springboot和eureka服务发现器不起作用
- python - 如何像指针一样使用 Python 函数 arg?
- python - 自动通过多个路径位置并导出文件名
- typescript - vscode API:每X秒调用一次函数
- gradle - 将属性传递给 Gradle Tooling API ProjectConnection
- wordpress - 监督事件侦听器到 docker 容器中的程序或组的特定列表
- reactjs - 如何将条件样式类添加到 React AsyncTypeahead
- r - 如何将字符串转换为数字以对 R 中的一个单元格中的值求和?