I want to create a DataGrid
control in WPF in which there is a button in the first cell of each row. Clicking this button will show RowDetailsTemplate
or the SubRow.
How do I add a button which will show/Hide the RowDetailsTemplate
?
First create a DataGridTemplateColumn
to contain the button:
<DataGridTemplateColumn> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Button Click="ShowHideDetails">Details</Button> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn>
When the button is clicked, update the containing DataGridRow
's DetailsVisibility
:
void ShowHideDetails(object sender, RoutedEventArgs e) { for (var vis = sender as Visual; vis != null; vis = VisualTreeHelper.GetParent(vis) as Visual) if (vis is DataGridRow) { var row = (DataGridRow)vis; row.DetailsVisibility = row.DetailsVisibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible; break; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With