wpf RowDetailsTemplate展开折叠例子
时间: 2024-06-13 20:03:40 浏览: 227
在WPF中,RowDetailsTemplate是一种用于在DataGrid控件中显示行详细信息的模板。下面是一个展开折叠例子:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid Name="dgUsers" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<DataGridTextColumn Header="Age" Binding="{Binding Age}" />
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Details}" Margin="10" />
<Button Content="Close" Click="Button_Click" />
</StackPanel>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
</Grid>
</Window>
在这个例子中,我们使用了一个StackPanel来包含行详细信息和一个关闭按钮。当用户单击行时,行详细信息将展开,当用户再次单击行时,行详细信息将折叠起来。同时,我们还添加了一个关闭按钮,以便用户可以手动关闭行详细信息。
private void dgUsers_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
DataGridRow row = sender as DataGridRow;
if (row.DetailsVisibility == Visibility.Collapsed)
row.DetailsVisibility = Visibility.Visible;
else
row.DetailsVisibility = Visibility.Collapsed;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Button button = sender as Button;
DataGridRow row = FindVisualParent<DataGridRow>(button);
row.DetailsVisibility = Visibility.Collapsed;
}
阅读全文