在工具提示中添加断裂线

Gal*_*led 53 wpf xaml tooltip

¿如何在XAML的工具提示中为文本添加断行线?

我尝试这个:

        <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>
Run Code Online (Sandbox Code Playgroud)

但不起作用:

小智 95

我觉得有用的另一种方法是嵌入&#x0a;工具提示中.此时工具提示将有一个Linebreak.例如

ToolTip="Host name or IP address of the server. Click the &#x0a;Find Server button to help obtain the correct entry."
Run Code Online (Sandbox Code Playgroud)

这允许xaml代码更简洁,但可能不太可读.Newline in string属性中的更多细节.


HCL*_*HCL 69

<Label>
  <Label.ToolTip> 
     <TextBlock>
          Lorem ipsum dolor sit amet,
          <LineBreak /> 
          consectetur adipiscing elit. 
      </TextBlock> 
  </Label.ToolTip> 
</Label>
  ....
Run Code Online (Sandbox Code Playgroud)


Nic*_*las 17

更紧凑:

<Label TooTip="Line1 &#10; Line2" />
Run Code Online (Sandbox Code Playgroud)


Rac*_*hel 13

将您的物品包裹在StackPanel中,StackPanel将它们叠加在另一个上面

您现在拥有的内容将无法编译,因为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>
Run Code Online (Sandbox Code Playgroud)

  • @Galled如果您只使用Text,那就什么都没有.我实际上给了HCL的答案+1,因为这是向文本字段添加换行符的正确方法.如果你正在混合其他UI元素,或者你想在每一行上使用特殊格式(我还用它来说明为什么你的代码示例不能编译),你只想使用StackPanel. (4认同)

Tk1*_*993 6

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

label1.ToolTip="Line1" + Environment.NewLine + "Line2";
Run Code Online (Sandbox Code Playgroud)

  • 快速评论,今天尝试使用这个解决方案,因为我需要在 cs 中设置它,而不是 xaml,并发现它拼写错误:Enviroment 缺少一个“n”。应该是“环境”。希望你能解决这个问题 (2认同)
  • Environment.Newline 应该是Environment.NewLine(大写L) (2认同)