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

Grids for WPF Forum

Posted 5 months ago by Marek Pšenka
Version: 19.1.0683
Avatar

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}" >
    <DataGridTemplateColumn.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
             <Style.Triggers>
                 <Trigger Property="IsSelected" Value="True">
                     <Setter Property="Background" Value="{DynamicResource {x:Static themes:AssetResourceKeys.ListBackgroundNormalBrushKey}}"/>
                 </Trigger>
             </Style.Triggers>
        </Style>
    </DataGridTemplateColumn.CellStyle>
</DataGridTemplateColumn>

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.

Marek

Comments (2)

Posted 5 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Marek,

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

https://github.com/Actipro/WPF-Controls/blob/develop/Source/DataGrid.Contrib/Themes/Generic.xaml

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 5 months ago by Marek Pšenka
Avatar

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="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:shared="http://schemas.actiprosoftware.com/winfx/xaml/shared"
                    xmlns:themes="http://schemas.actiprosoftware.com/winfx/xaml/themes">
    <ControlTemplate x:Key="NoHighlightCellControlTemplate" TargetType="{x:Type DataGridCell}">
        <ControlTemplate.Resources>
            <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
        </ControlTemplate.Resources>
        <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}"
                              SnapsToDevicePixels="true">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition MaxHeight="11" />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <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}" />
            </Grid>
        </shared:ElementChrome>
    </ControlTemplate>

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

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

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 (v22.1.2) was released 1 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.