单击DataGrid行时显示详细信息

时间:2012-03-29 06:35:50

标签: wpf

我有一个DataGrid,显示教授的课程列表。我想做的是让教授点击DataGrid上的一行,然后显示该班级的学生。

WPF有可能吗?

2 个答案:

答案 0 :(得分:1)

在数据网格中,添加以下部分:

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

答案 1 :(得分:0)

如果您想在表格中内嵌显示详细信息,@ AnsonWoody会写下答案。如果您想在单独的控件中显示详细信息,请使用SelectedItem的{​​{1}}或DateGrid的{​​{1}}。

假设您的datacontext包含CurrentItem中的项目且每个项目都有属性CollectionViewSource,您可以执行以下操作:

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> 是一个集合,请使用ContentControl之类的内容或任何适合您需求的内容,以便获得漂亮的学生代表。

相关问题