Reputation: 4206
¿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:
Upvotes: 85
Views: 44543
Reputation: 523
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.
Upvotes: 2
Reputation: 4381
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.
Upvotes: 1
Reputation: 10404
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";
Upvotes: 2
Reputation: 329
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.
Upvotes: 2
Reputation: 1698
Another approach that I find useful is to embed 

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 
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.
Upvotes: 137
Reputation: 521
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";
Upvotes: 11
Reputation: 59
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.
Upvotes: 3
Reputation: 36815
<Label>
<Label.ToolTip>
<TextBlock>
Lorem ipsum dolor sit amet,
<LineBreak />
consectetur adipiscing elit.
</TextBlock>
</Label.ToolTip>
</Label>
....
Upvotes: 99
Reputation: 3369
You can do this :
<Label>
<Label.ToolTip>
<TextBlock>
Line1
<LineBreak/>
Line2
</TextBlock>
</Label.ToolTip>
</Label>
Upvotes: 4
Reputation: 132648
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>
Upvotes: 21