How do I stop DataGridCells in a single specific column from highlighting?

Grids for WPF Forum

Posted 2 years ago by Marek Pšenka
Version: 19.1.0683

I am trying to disable highlighting of cells in a DataGridTemplateColumn of a ThemedDataGrid. My first attempt was to set the background based on the DataGridCell.IsSelected property via Trigger in the Cell style:

<DataGridTemplateColumn CellTemplate="{StaticResource SeparatorCellTemplate}" >
        <Style TargetType="{x:Type DataGridCell}">
                 <Trigger Property="IsSelected" Value="True">
                     <Setter Property="Background" Value="{DynamicResource {x:Static themes:AssetResourceKeys.ListBackgroundNormalBrushKey}}"/>

The style works and the Background property changes when IsSelected is True, but it has no effect - the Cell still highlights blue. I also tried to set the IsFocusable property to False to no effect.

I Snooped the visual tree and found that it is actually the ElementChrome that renders blue as specified by its BackgroundFocused property. I think i have to change the chrome. I can see it as a child of each cell in the grid column in the Snoop visual tree but I have no idea how to do it in code, preferrably in XAML. Are my assumptions correct? Is it possible to conditionally modify the ElementChrome? Is there a cleaner way to do this?

I would love to include a screenshot which I think explains a lot, but I do not know how.

Thank you very much for your assistance.


Comments (2)

Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA

Hi Marek,

Our styles for the Microsoft DataGrid control are all open source here:

Yes that is correct, the ElementChrome is what triggers the selection appearance.  You could clone our default cell Style/Template and update it however you need (and then point DataGrid.CellStyle to the updated Style you made), or you could override our ListItemBackgroundSelectedNormalBrushKey brush in the DataGrid.Resources to be something like Transparent.

Either of those should work.

Actipro Software Support

Answer - Posted 2 years ago by Marek Pšenka

Thanks a lot for the clues!

I managed to solve the problem by cloning the DataGridCell ControlTemplate as per your first suggestion. For those interested in the complete solution, I factored it out into a ResourceDictionary.

<ResourceDictionary xmlns=""
    <ControlTemplate x:Key="NoHighlightCellControlTemplate" TargetType="{x:Type DataGridCell}">
            <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
        <shared:ElementChrome x:Name="chrome" Background="{TemplateBinding Background}"
                              BackgroundDisabled="{DynamicResource {x:Static themes:AssetResourceKeys.ListItemBackgroundDisabledBrushKey}}"
                              BackgroundHover="{DynamicResource {x:Static themes:AssetResourceKeys.ListItemBackgroundHoverBrushKey}}"
                              BackgroundFocused="{DynamicResource {x:Static themes:AssetResourceKeys.ListItemBackgroundNormalBrushKey}}"
                              BorderBrush="{TemplateBinding BorderBrush}"
                              BorderBrushDisabled="{DynamicResource {x:Static themes:AssetResourceKeys.ListItemBorderDisabledBrushKey}}"
                              BorderBrushHover="{DynamicResource {x:Static themes:AssetResourceKeys.ListItemBorderHoverBrushKey}}"
                              BorderBrushFocused="{DynamicResource {x:Static themes:AssetResourceKeys.ListItemBorderNormalBrushKey}}"
                              BorderThickness="{TemplateBinding BorderThickness}"
                              InnerBorderThickness="{DynamicResource {x:Static themes:AssetResourceKeys.ListItemInnerBorderNormalThicknessKey}}"
                              CornerRadius="{TemplateBinding themes:ThemeProperties.CornerRadius}"
                    <RowDefinition MaxHeight="11" />
                    <RowDefinition />
                <Rectangle x:Name="highlight"
                           Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(themes:ThemeProperties.IsGlassEnabled), Converter={StaticResource BooleanToVisibilityConverter}}" />
                <ContentPresenter Grid.RowSpan="2" Margin="{TemplateBinding Padding}"
                                  HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                  SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />

    <SolidColorBrush Color="Transparent" x:Key="TransparentBrush" />

    <Style x:Key="NoHighlightCellStyle" TargetType="{x:Type DataGridCell}">
        <Setter Property="Template" Value="{StaticResource NoHighlightCellControlTemplate}" />
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="{StaticResource TransparentBrush}" />
                <Setter Property="BorderBrush" Value="{StaticResource TransparentBrush}" />

Just apply it to cells in the column where you want highlighting disabled like so:

<DataGridTemplateColumn CellStyle="{StaticResource NoHighlightCellStyle}"
                        CellTemplate="{StaticResource SeparatorCellTemplate}" />
The latest build of this product (v24.1.2) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.