Add a breakline in tooltip

34,913

Solution 1

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

Solution 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

More compact:

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

Solution 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

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";
Share:
34,913
Galled
Author by

Galled

I'm an engineer in statistics and computer science. My every day is work with Transact-SQL and R. I love my children, programming in JASS, draw comics, write stories and my work, in that order :D Currently I work in the Soluciones.

Updated on July 25, 2022

Comments

  • Galled
    Galled almost 2 years

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

  • Galled
    Galled over 12 years
    Your answer and Rachel's answer works, but I don't know wich is better.
  • Galled
    Galled over 12 years
    Which is the benefit of use <stackpanel /> against a simple <textblock /> with <linebreaks />?
  • HCL
    HCL over 12 years
    @Galled: Depends on what you want to do. If you only want to have some text formatted with cr, the mine is the better. You will have less trouble with formatting (Line Distances, auto-Cr etc.). However there are situations where you want explicitely more than one TextBlock. In this case, Rachel's solution is better. It's dependengin on the target. For your XAML-example I would cleary only take one TextBlock.
  • Rachel
    Rachel over 12 years
    @Galled Nothing if you're only working with Text. I actually gave HCL's answer a +1 because it's the correct way to add line breaks to a text field. You'd only want to use a StackPanel if you're mixing other UI Elements, or if you wanted special formatting on each of your lines (I also used it to show why your code example wouldn't compile)
  • Malavos
    Malavos about 10 years
    Hey Chris (and everyone). Good that I have found this question and answer. You see, as I'm using localization, and I'm dynamically loading everything from it, how can I insert a line into this tooltip? See for instance: comboBoxItemLightTheme.ToolTip = Lang.Language.toolTipVisualComboBoxThemeLight_PT; Which is loading from a resource a string, let's say this tooltip has: Bacon ipsum dolor sit amet tri-tip boudin drumstick brisket leberkas pork loin shank. How can I add a newline after "Shank", for instance?
  • stephen
    stephen over 9 years
    A caveat: should be ParentTag.ToolTip as in Label.ToolTip.
  • ANeves
    ANeves about 9 years
    @Malavos ask that in a new question. :)
  • Mario Garcia
    Mario Garcia over 7 years
    Quick comment, tried to use this solution today, as I needed to set it in cs, not xaml, and found out that it is miss-spelled: Enviroment lacks one 'n'. It should be Environment. Hope you can fix that
  • Tk1993
    Tk1993 almost 7 years
    what if I want to display &#x0a; as a text in the tooltip?
  • James Wilkins
    James Wilkins over 6 years
    @TusharKukreti XAML is just using entities like XML/HTML does, so knowing this, it should be easy to figure out that all you need to do is escape the & character using &amp; (ampersand) to get &amp;#x0a;.
  • Mgamerz
    Mgamerz over 6 years
    Environment.Newline should be Environment.NewLine (capital L)
  • EJoshuaS - Stand with Ukraine
    EJoshuaS - Stand with Ukraine over 5 years
    @Tk1993 Why would you want to display that as literal text? Have you ever had an actual need to do anything like that?
  • Zam
    Zam over 4 years
    LineBreak cannot be used inside TextBox ToolTip
  • Zam
    Zam over 4 years
    LineBreak cannot be used inside TextBox --> ToolTip
  • Zam
    Zam over 4 years
    Yes, I did. If we want to discuss it, we should go to chat room. Comments are wrong place for discussion
  • Paul Demesa
    Paul Demesa over 4 years
    Show your code, instead. I just tested this in VS2019. If you check the thread, Steven Muir also said the same thing back in 2011. If it doesn’t work in your case, there’s something wrong with your set up.
  • Paul Demesa
    Paul Demesa over 4 years
    Zam, are you using any recent version of Visual Studio? Or some third-party XAML tool? If the latter, it’s possible its XAML parser isn’t up to date. This feature has been out for years. Try any Visual Studio community edition, it’s a free download.
  • Zam
    Zam over 4 years
    No 3rd party tools. VS2017 with latest SP.
  • Paul Demesa
    Paul Demesa over 4 years
    I tested it with VS2019. And I’ve used this technique since the RUN feature was released years ago. Like I said, show your code and we’ll test it. I still believe there’s something wrong with your set up. What's the exact error message?
  • Zam
    Zam over 4 years
    there are no error message. just linebreaks does works -- text in tool tip showing in one line. NOTE: I am using TEXTBOX, not TEXTBLOCK.
  • Paul Demesa
    Paul Demesa over 4 years
    Jeez, post in the correct place. You're trying to correct a working code, with something that meets a different set of requirements. You confuse people that way.
  • Tatranskymedved
    Tatranskymedved almost 3 years
    copy of the approved answer
  • Piotr Golacki
    Piotr Golacki over 2 years
    The disadvantage of that is when you are formatting your code and you add a new line in your code by pressing Enter when ToolTip text is quite long and you want to break it up to several lines this would cause problems such as adding lots of whitespace into the displayed text. But for short ToolTips it's nice and concise.