Styling columns based on DataGridTemplateColumn in a WPF DataGrid

Posted by nareshbhatia on Stack Overflow See other posts from Stack Overflow or by nareshbhatia
Published on 2010-04-25T18:10:19Z Indexed on 2010/04/25 18:13 UTC
Read the original article Hit count: 625

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about wpf

Related posts about datagridtemplatecolumn