我有DataGrid
个Person
个DataGrid
个对象RowDetailsTemplate
,其EntityFramework
包含所选人员的工作,我正在使用DataContext
来生成PersonWork
,每个人至少有一个作业(因此Person包含另一个类型为RowDetails
的对象的外键)。
为了使用DataGrid
工作填充selectedPerson's
itemSource
,我将其(RowDetailsTemplate)Person
绑定到RowDetails
类的导航属性(由EF生成),但SelectedPerson.PersonWork
网格始终为空! (当我使用它包含记录的即时窗口检查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>
代码:
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; }
}
EF生成的类人员看起来像这样:
ObservableCollection
Ps:我使用的是EF 6.1.1
更新
ListPersons是var _dbContext=new DBEntities();
ListPersons= new ObservableCollection<Person>(_dbContext.Persons);
,它实例化如下:
{{1}}
答案 0 :(得分:0)
为了实现这一点,您还必须在内部DataGrid中指定DataContext或使用ElementName直接从Main DataGrid获取内部DataGrid Navigation属性集合,以下代码可以正常工作:
<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>