RowDetailsTemplate ItemSource Binded to an EF NavigationProperty

只谈情不闲聊 提交于 2019-12-25 04:49:07

问题


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:


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>


来源:https://stackoverflow.com/questions/27131113/rowdetailstemplate-itemsource-binded-to-an-ef-navigationproperty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!