'Add a breakline in tooltip

¿How can I add a breakline to a text inside a tooltip in XAML?

I try with this:

        <Label Name="label4" UseLayoutRounding="False" Focusable="False" AllowDrop="False" Foreground="Black" Margin="6,44,132.027,76" ToolTipService.ShowDuration="12000">
                <Label.ToolTip>
                    <ToolTip>
                    <TextBlock>Lorem ipsum dolor sit amet, consectetur adipiscing elit. </TextBlock>
                    <TextBlock>Suspendisse eget urna eget elit ullamcorper tincidunt. Sed nec arcu sed ante sodales </TextBlock>
                    <TextBlock>Pellentesque elit libero, semper ac tincidunt vitae, euismod at ligula.</TextBlock>
                    </ToolTip>
                </Label.ToolTip>
            <Label.Content>
                <TextBlock TextAlignment="Right" TextWrapping="Wrap" Height="19" Width="108" >Lorem Ipsum</TextBlock>
            </Label.Content>
        </Label>

But doesn't works:



Solution 1:[1]

<Label>
  <Label.ToolTip> 
     <TextBlock>
          Lorem ipsum dolor sit amet,
          <LineBreak /> 
          consectetur adipiscing elit. 
      </TextBlock> 
  </Label.ToolTip> 
</Label>
  ....

Solution 2:[2]

Another approach that I find useful is to embed &#x0a; in the tooltip. The Tooltip will then have a Linebreak at this point. For example

ToolTip="Host name or IP address of the server. Click the &#x0a;Find Server button to help obtain the correct entry."

This allows the xaml code to be more concise, but perhaps less readable. More details at Newline in string attribute.

Solution 3:[3]

More compact:

<Label TooTip="Line1 &#10; Line2" />

Solution 4:[4]

Wrap your items in a StackPanel, which will stack them one on top of the other

What you have now won't compile because ToolTips can only have 1 child object, and you are trying to add 3

<Label Name="label4" UseLayoutRounding="False" Focusable="False" AllowDrop="False" Foreground="Black" Margin="6,44,132.027,76" ToolTipService.ShowDuration="12000">
    <Label.ToolTip>
        <StackPanel>
            <TextBlock>Lorem ipsum dolor sit amet, consectetur adipiscing elit. </TextBlock>
            <TextBlock>Suspendisse eget urna eget elit ullamcorper tincidunt. Sed nec arcu sed ante sodales </TextBlock>
            <TextBlock>Pellentesque elit libero, semper ac tincidunt vitae, euismod at ligula.</TextBlock>
        </StackPanel>
    </Label.ToolTip>
    <Label.Content>
        <TextBlock TextAlignment="Right" TextWrapping="Wrap" Height="19" Width="108" >Lorem Ipsum</TextBlock>
    </Label.Content>
</Label>

Solution 5:[5]

Above answers are only for xaml code. If you want to add new line in CS code , use "Environment.Newline"

label1.ToolTip="Line1" + Environment.NewLine + "Line2";

Solution 6:[6]

You can do this :

<Label>
<Label.ToolTip>
    <TextBlock>  
      Line1
      <LineBreak/>
     Line2
  </TextBlock>
</Label.ToolTip>
</Label>

Solution 7:[7]

Here’s a variation of the line feed approach:

<Label.ToolTip>
    <TextBlock>
        <Run Text=”Line1”/>
        <LineBreak/>
        <Run Text=”Line2”/>
    </TextBlock>
</Label.ToolTip>

The advantage of this is that each line can have its own style.

Solution 8:[8]

Even though you are looking for a XAML solution - here another C# solution:

If you plan by any chance to outsource your strings including tooltips to a resource file (.resx) - for example for multi language support - you can literally do line breaks in your values in this resource file e.g. with Shift+Enter and they will also occur in the view.

Solution 9:[9]

From the code behind with a binding for example.

You can use this class:

Environment.NewLine

like this:

labelName.Tooltip = $"line1 {Environment.NewLine} line2 {Environment.NewLine} line3";

Solution 10:[10]

There is also different approach. You can tell XAML that you want to keep same spacing as you wrote it down. This can be done by setting attribute xml:space="preserve" to parent element - see MSDN page for details.

<Label>
  <Label.ToolTip>
    <ToolTip xml:space="preserve">My main line,
main line on 2nd row.

another line with space in between         and some spacing.</ToolTip>
  </Label.ToolTip> 
</Label>

This allows you to also use new lines, tabs and multiple-spaces next to each other, they won't be removed.

Solution 11:[11]

Although this question is pretty old, someone might stumble along here, searching for a similar problem. This simple code helps with dynamically filled tooltips and is also language and culture aware. What I like most is the fact that you need not know anything about the content of the tooltip, neither its length, nor how many lines might be required to display it nicely:

<Label Content="This label">
    <ToolTipService.ToolTip>
        <TextBlock Text="Long tooltip text, which will wrap, if the tooltip box with iss reached"
                   TextWrapping="Wrap" MaxWidth="300" />
    </ToolTipService.ToolTip>
</Label>

Or inject the text from Resources.resx:

<TextBox Text="{Binding Path=Filepath, UpdateSourceTrigger=PropertyChanged}">
    <ToolTipService.ToolTip>
        <TextBlock Text="{x:Static resources:Resources.Tooltip_Filepath}"
                   TextWrapping="Wrap" MaxWidth="300" />
    </ToolTipService.ToolTip>
</TextBox>

I hope this helps someone.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 stephen
Solution 2 Vimes
Solution 3 Nicolas
Solution 4 Rachel
Solution 5
Solution 6 Steven Muhr
Solution 7 Paul Demesa
Solution 8
Solution 9 A. Morel
Solution 10 Tatranskymedved
Solution 11 Roman