RowDetailsTemplate ItemSsource绑定到EF导航属性

时间:2014-11-25 15:50:31

标签: c# wpf entity-framework wpfdatagrid

我有DataGridPersonDataGrid个对象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}}

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>