RowDetailsTemplate ItemSource绑定到EF NavigationProperty [英] RowDetailsTemplate ItemSource Binded to an EF NavigationProperty
问题描述
我有一个 DataGrid
的 Person
对象与另一个 DataGrid
在其 RowDetailsTemplate
中包含所选人员的作业,我使用 EntityFramework
来生成 DataContext
,每个人至少有一个作业(所以Person包含另一个类型为 PersonWork
的对象的外键)。
为了将 RowDetails
DataGrid
与 selectedPerson的
工作,我已将其(RowDetailsTemplate) itemSource
绑定到 Person
类的导航属性由EF生成),但$ code> RowDetails Grid始终为空! (当我使用包含记录的立即窗口检查 SelectedPerson.PersonWork
)
I have a DataGrid
of Person
objects with another DataGrid
in its RowDetailsTemplate
which contains the selected person's Jobs, I am using EntityFramework
to generate the DataContext
, each person has a least one job (so Person contains a foreign key to another object of type PersonWork
).
In order to populate the RowDetails
DataGrid
with the selectedPerson's
works, I've bind its (the RowDetailsTemplate) itemSource
to the navigation property of the Person
class (generated by EF), but the RowDetails
Grid is always empty! (when i inspect the SelectedPerson.PersonWork
using the immediate window it contains records)
这里 Xaml
我使用的代码:
<DataGrid Style="{StaticResource DataGridStyle}" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding ListPersons}" SelectedItem="{Binding SelectedPerson,Mode=TwoWay}" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding PersonName}" Header="Name" Width="SizeToHeader" MinWidth="100"/>
<DataGridTextColumn Binding="{Binding PersonAge}" Header="Age" Width="SizeToHeader" MinWidth="100"/>
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate >
<DataGrid Height="100" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding Path=SelectedPerson.PersonWorks}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding WorkID}" Header="WorkID" Width="SizeToHeader" MinWidth="100"/>
<DataGridTextColumn Binding="{Binding WorkTitle}" Header="Title" Width="SizeToHeader" MinWidth="100"/>
<DataGridTextColumn Binding="{Binding WorkRecommandation}" Header="Recommandation" Width="SizeToHeader" MinWidth="300"/>
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
而EF生成的类人员如下所示:
And The Class Person generated by EF looks like so:
public partial class Person
{
public Person()
{
this.PersonWorks = new HashSet<PersonWorks>();
}
public long PersonId { get; set; }
public string PersonName { get; set; }
public long PersonAge { get; set; }
public virtual ICollection<PersonWork> PersonWorks { get; set; }
}
Ps:我正在使用EF 6.1.1
Ps: I am using EF 6.1.1
更新
ListPersons是一个 ObservableCollection
,它像这样实例化:
Update
The ListPersons is an ObservableCollection
and it is instantiated like so :
var _dbContext=new DBEntities();
ListPersons= new ObservableCollection<Person>(_dbContext.Persons);
推荐答案
为了达到这一点,你还必须指定DataContext在内部DataGrid中,或者使用ElementName直接从Main DataGrid获取内部的DataGrid Navigation属性集合,以下代码完美地工作:
In order to achieve that you must also specify the DataContext in inner DataGrid or get the inner DataGrid Navigation property collection directly from the Main DataGrid using the ElementName, the following code work perfectly :
<DataGrid x:Name="DataGrid" Style="{StaticResource DataGridStyle}" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding ListPersons}" SelectedItem="{Binding SelectedPerson,Mode=TwoWay}" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding PersonName}" Header="Name" Width="SizeToHeader" MinWidth="100"/>
<DataGridTextColumn Binding="{Binding PersonAge}" Header="Age" Width="SizeToHeader" MinWidth="100"/>
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate >
<DataGrid Height="100" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding Path=SelectedItem.PersonWorks, ElementName=DataGrid}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding WorkID}" Header="WorkID" Width="SizeToHeader" MinWidth="100"/>
<DataGridTextColumn Binding="{Binding WorkTitle}" Header="Title" Width="SizeToHeader" MinWidth="100"/>
<DataGridTextColumn Binding="{Binding WorkRecommandation}" Header="Recommandation" Width="SizeToHeader" MinWidth="300"/>
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
这篇关于RowDetailsTemplate ItemSource绑定到EF NavigationProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!