Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I just need a simple WPF ToolTip Style that displays multiple lines

Due to all the noise about fancy, super, huge, and blah, blah, blah, tooltips, I cannot find the answer.

I just need a simple style that sets TextWrapping="Wrap" and allows me to set a width.

One that duplicates the existing / default style, but just word wraps.

like image 525
AMissico Avatar asked Jun 18 '12 21:06

AMissico


2 Answers

<Window.Resources>
    <Style TargetType="{x:Type ToolTip}">
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <TextBlock TextWrapping="Wrap" Text="{Binding}" />
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <Rectangle Width="100" Height="100" Fill="Red">
        <Rectangle.ToolTip>
            <ToolTip Width="100">
                This is some text with text wrapping.
            </ToolTip>
        </Rectangle.ToolTip>
    </Rectangle>
</Grid>

This example is assuming you want to be able to set the width on a per-usage basis. If you want to set it as part of the style, add it to the TextBlock element.

like image 148
Trevor Elliott Avatar answered Sep 26 '22 05:09

Trevor Elliott


This style prevents a tooltip from popping up on empty strings.

<Style TargetType="ToolTip">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToolTip">
                <TextBlock Text="{TemplateBinding Content}"
                           MaxWidth="400"
                           TextWrapping="Wrap"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="Content" Value="">
            <Setter Property="Visibility" Value="Collapsed" />
        </Trigger>
    </Style.Triggers>
</Style>

Or using ContentTemplate:

<Style TargetType="{x:Type ToolTip}">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding}"
                               MaxWidth="400"
                               TextWrapping='Wrap' />
                </StackPanel>
            </DataTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="Content" Value="">
            <Setter Property="Visibility" Value="Collapsed" />
        </Trigger>
    </Style.Triggers>
</Style>
like image 20
Nikolai Koudelia Avatar answered Sep 24 '22 05:09

Nikolai Koudelia