Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Common Tooltip style in WPF

Can I make a tooltip style which can be applied to all tooltips for every control?

I tried this but I cant get the content (Tooltip text) in style, it is showing empty text in tooltip:

<Style TargetType="{x:Type ToolTip}" >
        <Setter Property="OverridesDefaultStyle" Value="true" />
        <Setter Property="HasDropShadow" Value="True" />
        <Setter Property="Foreground" Value="White" />
        <Setter Property="FontSize" Value="12" />
        <Setter Property="Placement" Value="Bottom" />
        <Setter Property="VerticalOffset" Value="0" />
        <Setter Property="Padding" Value="8" />

        <Setter Property="HorizontalContentAlignment" Value="Center" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToolTip}" >
                    <StackPanel Margin="7,1" >
                      <Border Background="#FFF7F7CC" CornerRadius="1" >
                            <TextBlock Margin="1" Foreground="Black" HorizontalAlignment="Center" VerticalAlignment="Top" Text="{TemplateBinding ToolTip}"/>
                        </Border>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

For Using this style I have to put a seperate Tooltip tag in control, eg to apply tooltip to border,

<Border>
    <Border.ToolTip>
          <ToolTip ToolTip="This is tooltip text"  />
    </Border.ToolTip>
........
.........
</Border>

but is there any way where tooltipstyle applies to all control with tooltip mentioned in same tag. eg.

<Border BorderBrush="Transparent" Background="Transparent" Cursor="Help" ToolTip="This is Tooltip" >
.....
.....
</Border>

let me know if any further details are required. Thanks in Anticipation.

like image 263
Hardik Avatar asked Jun 21 '13 06:06

Hardik


People also ask

What is ToolTip in WPF?

A tooltip is a small pop-up window that appears when a user pauses the mouse pointer over an element, such as over a Button. This topic introduces the tooltip and discusses how to create and customize tooltip content.

How do I add tooltip to XAML?

Use the Placement property or ToolTipService. Placement attached property to place the ToolTip above, below, left, or right of the pointer. You can set the VerticalOffset and HorizontalOffset properties to change the distance between the pointer and the ToolTip.

What is TextBlock WPF?

The TextBlock control provides flexible text support for UI scenarios that do not require more than one paragraph of text. It supports a number of properties that enable precise control of presentation, such as FontFamily, FontSize, FontWeight, TextEffects, and TextWrapping.


1 Answers

Yes Your approach will work. But a small change is needed in the Control Template. Replace the TextBlock with ContentPresenter.

                <ControlTemplate TargetType="{x:Type ToolTip}" >
                    <StackPanel Margin="7,1" >
                        <Border Background="#FFF7F7CC" CornerRadius="1" >
                        <ContentPresenter Margin="1" HorizontalAlignment="Center" VerticalAlignment="Top" />
                        </Border>
                    </StackPanel>
                </ControlTemplate>
like image 183
Jawahar Avatar answered Nov 04 '22 13:11

Jawahar