RowDetailsTemplate ItemSource Binded to an EF Navi

2019-09-06 18:06发布

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)

Here the Xaml Code i use:

<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>

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: I am using EF 6.1.1

Update The ListPersons is an ObservableCollectionand it is instantiated like so :

var _dbContext=new DBEntities();
ListPersons= new ObservableCollection<Person>(_dbContext.Persons);

1条回答
smile是对你的礼貌
2楼-- · 2019-09-06 18:15

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>
查看更多
登录 后发表回答