Styling columns based on DataGridTemplateColumn in a WPF DataGrid
- by nareshbhatia
I am using a WPF DataGrid where one of the columns has a requirement to show an "Edit" hyperlink if the row is editable - this is indicated by a boolean flag in the backing model for the row. I was able to achieve this using a DataGridTemplateColumn - no problems. However an additional requirement on the entire row is not to show any highlights when the row is selected (this is a blue background by default). I have been able to achieve this on other columns by defining the DataGridCell style with a transparent background, e.g.
<DataGridTextColumn
Header="Id"
Binding="{Binding Path=Id}"
HeaderStyle="{StaticResource DataGridColumnHeaderStyle}"
CellStyle="{StaticResource DataGridCellStyle}" />
where DataGridCellStyle is defined as follows:
<Style x:Key="DataGridCellStyle" TargetType="{x:Type DataGridCell}">
<Setter Property="Background" Value="Transparent" />
...
</Style>
However the column in question, a DataGridTemplateColumn, does not offer a "CellStyle" attribute which I can use for turning off selection highlights. So my question is how to set the cell style when using a DataGridTemplateColumn? Here's my implementation of the column which satisfies the first requirement (i.e. showing an "Edit" hyperlink if the row is editable):
<DataGridTemplateColumn
Header="Actions"
HeaderStyle="{StaticResource CenterAlignedColumnHeaderStyle}">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock
Visibility="{Binding Path=Editable, Converter={StaticResource convVisibility}}"
Style="{StaticResource CenterAlignedElementStyle}">
<Hyperlink
Command="..."
CommandParameter="{Binding}">
<TextBlock Text="Edit" />
</Hyperlink>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Thanks.