Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a Button to a WPF DataGrid

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?

like image 445
Ulhas Tuscano Avatar asked Jun 15 '10 14:06

Ulhas Tuscano


1 Answers

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;     } } 
like image 103
Ray Burns Avatar answered Sep 20 '22 04:09

Ray Burns