Dominic Jonas
Dominic Jonas

Reputation: 5005

Show ToolTip (with custom style) of TextBlock Text

I've got the following problem. I have a DataGrid with a DataGridTemplateColumn and I want to display the text of a cell with a popup (because it can be trimmed).

This is my xaml code:

                    <DataGridTemplateColumn>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock 
                                    Text="{Binding Description}" 
                                    TextTrimming="CharacterEllipsis">
                                    <TextBlock.ToolTip>
                                        <ToolTip Style="{StaticResource ToolTipBrowserDescription}" ToolTip="test"/>
                                    </TextBlock.ToolTip>
                                </TextBlock>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

Style:

<Style TargetType="{x:Type ToolTip}" x:Key="ToolTipBrowserDescription">
    <Setter Property="DataContext" Value="{Binding RelativeSource={x:Static RelativeSource.Self}}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ToolTip}">
                <Border BorderBrush="{StaticResource HT_Background_DarkGrey}" Background="{StaticResource HT_Background_LightGrey3}" BorderThickness="1">
                    <TextBlock Text="{Binding ToolTip}" FontWeight="Bold" TextWrapping="Wrap" Margin="5" MinWidth="50" MaxWidth="{TemplateBinding MaxWidth}"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

tooltip1

But when I'm setting the ToolTip Property from "test" to "{Binding Description}" the ToolTip (which is shown) is empty.

Has anyone the solution for me. I'm stuck for about 2h..

Upvotes: 3

Views: 2617

Answers (1)

lokusking
lokusking

Reputation: 7456

This should do the trick.

ToolTipStyle:

<Style x:Key="ToolTipBrowserDescription"
       TargetType="{x:Type ToolTip}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ToolTip}">
                <Border BorderBrush="{StaticResource HT_Background_DarkGrey}" 
                        Background="{StaticResource HT_Background_LightGrey3}"
                        BorderThickness="1">
                    <TextBlock Text="{TemplateBinding Content}"
                               FontWeight="Bold"
                               TextWrapping="Wrap"
                               Margin="5"
                               MinWidth="50"
                               MaxWidth="{TemplateBinding MaxWidth}" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

TextBlock:

<TextBlock Text="{Binding Description}">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="ToolTip">
                <Setter.Value>
                    <ToolTip Style="{StaticResource ToolTipBrowserDescription}"
                             Content="{Binding Description}"/>
                </Setter.Value>
            </Setter>
        </Style>
    </TextBlock.Style>
</TextBlock>

You should Template-Bind to content, instead to ToolTip of the ToolTip

Upvotes: 5

Related Questions