Add a breakline in tooltip

WpfXamlTooltip

Wpf Problem Overview


¿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:

Wpf Solutions


Solution 1 - Wpf

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 https://stackoverflow.com/questions/183406/xaml-newline-in-string-attribute.

Solution 2 - Wpf

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

Solution 3 - Wpf

More compact:

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

Solution 4 - Wpf

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 - Wpf

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 - Wpf

You can do this :

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

Solution 7 - Wpf

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 - Wpf

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 - Wpf

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 - Wpf

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 - Wpf

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGalledView Question on Stackoverflow
Solution 1 - WpfausadminView Answer on Stackoverflow
Solution 2 - WpfHCLView Answer on Stackoverflow
Solution 3 - WpfNicolasView Answer on Stackoverflow
Solution 4 - WpfRachelView Answer on Stackoverflow
Solution 5 - WpfTk1993View Answer on Stackoverflow
Solution 6 - WpfSteven MuhrView Answer on Stackoverflow
Solution 7 - WpfPaul DemesaView Answer on Stackoverflow
Solution 8 - WpfNiklas S.View Answer on Stackoverflow
Solution 9 - WpfA. MorelView Answer on Stackoverflow
Solution 10 - WpfTatranskymedvedView Answer on Stackoverflow
Solution 11 - WpfRomanView Answer on Stackoverflow