CopyPastor

Detecting plagiarism made easy.

Score: 1; Reported for: Exact paragraph match Open both answers

Possible Plagiarism

Plagiarized on 2020-10-11
by Rao Hammas

Original Post

Original - Posted on 2010-06-16
by Ray Burns



            
Present in both answers; Present only in the new answer; Present only in the old answer;

First part of your question
> How to add a button for each row in a Wpf datagrid ?
That's how you add a `button` foreach row in a `WPF DataGrid`.
<DataGridTemplateColumn> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Button Click="ShowHideDetails">Details</Button> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> So, you create a `DataGridTemplateColumn` and inside that you define `DataTemplate` and Inside that you can set `Button` or any other UI Element you want to show for each row e.g a `CheckBox`.
Now, Coming to second part of your question.
> How to add a particular style to it ?

Well you can define style for this button like any other button you do or you can assign it a `Style` resource or template. Something like below,
<DataGridTemplateColumn> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Button Click="ShowHideDetails" Background="White" Foreground="Black" BorderThickness="2" BorderColor="Black">Details</Button> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn>
Or assign a style resource.
<DataGridTemplateColumn> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Button Click="ShowHideDetails" Style="MyBtnStyle">Details</Button> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn>

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; } }

        
Present in both answers; Present only in the new answer; Present only in the old answer;