How to update background cell style in datagrid when changing template.

WPF Studio, Themes, and Shared Library for WPF Forum

Posted 4 years ago by Kristoffer
Version: 19.1.0685

I'm trying to implement a behavior so a tooltip is shown when the text in a cell is longer than what is visible. I have found a stackoverflow solution for this here:

The issue is that I'm not able to get the background style for this cell when the row is selected or hovered. It has the alternating row background. But it will not change background when hovered or selected. I have tried to create my own style triggers, but I'm not able to update Background with these either. Foreground and other properties however work fine.

Any idea? below is my code sample.

                <DataGridTextColumn Header="Name" Binding="{Binding Name}" IsReadOnly="True" Width="*">
                        <Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Static themes:DataGridResourceKeys.DataGridCellStyleKey}}">
                            <Setter Property="Template">
                                    <ControlTemplate TargetType="{x:Type DataGridCell}">
                                        <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Text}" TextTrimming="CharacterEllipsis">
                                                <ToolTip Visibility="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget, Converter={StaticResource TrimToVisConverter}}">
                                                        <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Text}"/>
                                <Trigger Property="IsSelected" Value="True">
                                    <Setter Property="Background" Value="Blue"/>
                                    <Setter Property="Foreground" Value="Red"/>

Comments (2)

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

Might I suggest something easier than altering the CellStyle?  If you use a DataGridTemplateColumn instead, our Shared Library has an AdvancedTextBlock control that is effectively like a TextBlock but shows a tooltip when the text is trimmed.  You could simply show your text value in that control via the DataTemplate in DataGridTemplateColumn.CellTemplate and it would be much simpler.

Actipro Software Support

Posted 4 years ago by Kristoffer

A very good suggestion. Exactly what I was trying to do. Thanks!

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.