Search code examples
wpfxamltooltip

Wpf Tooltip and Binding to Label Content


I have a lot of labels on my wpf App like this.

<Label Style="{StaticResource styleLabelTitle}">
        <TextBlock TextTrimming="CharacterEllipsis" Text="{localization:Translate geolocation_controls}">
        </TextBlock>
</Label>

I want to add a tooltip to show complete name when ellipsis is working. So i add the tooltip in the label style.

<Style x:Key="styleLabelTitle" TargetType="Label" x:Shared="False">
        <Setter Property="Foreground" Value="{StaticResource brushTextsForeground}"></Setter>
        <Setter Property="FontWeight" Value="Bold"></Setter>
        <Setter Property="FontFamily" Value="Consolas"></Setter>
        <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content}" />
</Style>

The problem is that I think when tooltip is appearing is changing the textblock parent. So the text is only appearing in the Tooltip and is removed from the original label. Any ideas?

Thanks in advance.


Solution

  • I finally came to a solution by doing a new style.

    <Style x:Key="styleLabelText" TargetType="{x:Type Label}"
         x:Shared="False">
        <Setter Property="Foreground" Value="{StaticResource brushTextsForeground}"></Setter>
        <Setter Property="FontWeight" Value="Normal"></Setter>
        <Setter Property="FontFamily" Value="Consolas"></Setter>
        <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Label}">
                    <TextBlock HorizontalAlignment="Center"    VerticalAlignment="Center" TextTrimming="CharacterEllipsis"
                            Text="{TemplateBinding Content}" />
                </ControlTemplate>
            </Setter.Value>
        </Setter> </Style>