2012-03-29 125 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}"/>之類的東西或任何適合您需要的內容,以獲得漂亮的學生表示。