wpf 在工具提示中添加特征线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8170842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:11:04  来源:igfitidea点击:

Add a breakline in tooltip

wpfxamltooltip

提问by Galled

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

?如何在 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:

但不起作用:

回答by ausadmin

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

我认为有用的另一种方法是嵌入&#x0a;到工具提示中。此时工具提示将有一个换行符。例如

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.

这允许 xaml 代码更简洁,但可能不太可读。更多细节在字符串属性中的换行符

回答by HCL

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

回答by Nicolas

More compact:

更紧凑:

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

回答by Rachel

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

将您的项目包裹在 StackPanel 中,这会将它们堆叠在另一个之上

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

您现在拥有的将无法编译,因为 ToolTips 只能有 1 个子对象,而您正在尝试添加 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>

回答by Tk1993

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

以上答案仅适用于 xaml 代码。如果要在 CS 代码中添加新行,请使用“Environment.Newline”

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

回答by Steven Muhr

You can do this :

你可以这样做 :

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

回答by Paul Demesa

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.

这样做的好处是每条线都可以有自己的风格。

回答by Niklas S.

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

即使您正在寻找 XAML 解决方案 - 这是另一个 C# 解决方案:

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+Enterand they will also occur in the view.

如果您计划有机会将您的字符串(包括工具提示)外包给资源文件 (.resx) - 例如用于多语言支持 - 您可以在此资源文件中的值中进行换行,例如使用Shift+ Enter,它们也会出现在风景。

回答by A. Morel

From the code behindwith a bindingfor example.

后面的代码结合对的例子。

You can use this class:

你可以使用这个类:

Environment.NewLine

like this:

像这样:

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