2012-03-29 98 views
3

我有一个DataGrid,它显示教授的类的列表。我想要做的是让教授点击DataGrid上的一行,然后显示该班级的学生。单击DataGrid行时显示详细信息

WPF可能吗?

+0

您可以在https://www.wpftutorial.net/DataGrid.html的“行详细信息”部分下找到有关该主题的基本信息, – hypnos 2016-12-12 07:35:52

回答

1

在DataGrid,添加以下内容:

<DataGrid.RowDetailsTemplate> 
    <DataTemplate> 
     <DataGrid ItemsSource="{Binding students}"> 
     </DataGrid> 
    </DataTemplate> 
</DataGrid.RowDetailsTemplate> 
0

如果你想内嵌显示在表中的细节,@AnsonWoody写的答案。如果要在单独的控件中显示外部详细信息,请使用CollectionViewSourceDateGridCurrentItemSelectedItem

假设你的datacontext中包含的项目在ClassesWithStudents和每个项目都有一个属性Students,你可以做到以下几点:

<StackPanel x:Name="panel1"> 
    <StackPanel.Resources> 
     <CollectionViewSource x:Key="classesCollection" Source="{Binding ClassesWithStudents}"/> 
    </StackPanel.Resources> 
    <DataGrid x:Name="dg1" ItemsSource="{Binding Source={StaticResource classesCollection}}"> 
    </DataGrid> 
    <!-- Bind current item with path=/ --> 
    <ContentControl Content="{Binding Source={StaticResource classesCollection},Path=/Students}"/> 
    <!-- Bind selected item --> 
    <ContentControl Content="{Binding ElementName=dg1,Path=SelectedItem.Students}"/> 
</StackPanel> 

Ofcourse的ContentControl只是一个占位符。如果Students是一个集合,请使用诸如<ItemsControl ItemsSource="{Binding Source={StaticResource classesCollection},Path=/Students}"/>之类的东西或任何适合您需要的内容,以获得漂亮的学生表示。