как связать четыре таблицы в одном datagridview wpf linq - PullRequest
0 голосов
/ 07 июня 2019

У меня есть одно представление данных, и я хочу связать его с четырьмя связанными таблицами друг с другом и фотография объясняет отношения отношения четырех таблиц

Я пытался связать его с 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>

Я ожидаю показать su.Name, b.TypeOfBranch, su.Code, l.Name, sc.NumberOfStudent в моем представлении данных, но не все данные отображаются

1 Ответ

0 голосов
/ 08 июня 2019

Установив ItemsSource = {Binding}, вы привязываете его к DataGrid.DataContext. Эта привязка все еще активна, когда вы устанавливаете значение в коде - поэтому ItemsSource всегда указывает на DataContext, который, как я полагаю, пуст.

У вас есть два варианта: Удалите привязку ItemsSource из вашей DataGrid в xaml или же установите DataContex вместо ItemsSource в коде позади.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...