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
Avatar

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: https://stackoverflow.com/questions/22709715/automatic-tooltip-when-text-is-trimmed-in-datagrid

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

Comments (2)

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

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
Avatar

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

The latest build of this product (v24.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.