wpf 如何在 XAML 声明的标签中的两个单词之间强制换行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1216077/
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 20:47:35  来源:igfitidea点击:

How do you force a line break between two words in a XAML-declared Label?

wpfxaml

提问by devuxer

Maybe I'm not using the right key words, but all my searches are coming up empty. How do you force a line break?

也许我没有使用正确的关键词,但我所有的搜索都是空的。你如何强制换行?

I can tell you that none of the following work:

我可以告诉你,以下任何一项都不起作用:

<Label
    Content="Line&br;Break:" />

<Label
    Content="Line<br />Break:" />

<Label
    Content="Line
    Break:" />

<Label
    Content="Line\nBreak:" />

Can someone share this closely guarded secret?

有人可以分享这个严密保密的秘密吗?

Thanks.

谢谢。



EDIT:

编辑:

Okay, never mind. I finallyfound it.

好的没关系。我终于找到了。

<Label
    Content="Line&#x0a;Break:" />

Definitely not easy to guess!

绝对不容易猜到!



EDIT 2:

编辑2:

Okay, and now to get the text to be right-justified, I went with this:

好的,现在为了使文本正确对齐,我采用了以下方法:

<Label>
    <TextBlock
        TextAlignment="Right"
        Text="Line&#x0a;Break:" />
</Label>

Thanks to Julien for the idea of using a TextBlock.

感谢 Julien 提出使用 TextBlock 的想法。

回答by Julien Poulin

If you only need to display text, you can use a TextBlockinstead of a Label:

如果您只需要显示文本,您可以使用 aTextBlock代替 a Label

<TextBlock>
  Line<LineBreak/>Break:
</TextBlock>

If you really need a Label(e.g. you need to respond to a click event), you can wrap the above code inside a Label.

如果你真的需要一个Label(例如你需要响应一个点击事件),你可以将上面的代码包装在一个Label.

回答by Tabrez Shaikh

If you want a new line in a label:

如果您想在标签中添加新行:

<Label Content="Lorem &#10;ipsum" />

("10" is the ascii number for newline)

(“10”是换行符的ASCII码)

or

或者

<Label Content="Lorem &#xA;ipsum" />

("A" is the ascii number for newline in hex)

(“A”是十六进制换行符的ASCII码)

回答by Daniel Earwicker

I'd do this:

我会这样做:

<StackPanel>
    <Label>First line</Label>
    <Label>Second line</Label>
</StackPanel>

If the formatting gets really involved, I'd use FlowDocumentScrollViewer.

如果格式真的涉及到,我会使用FlowDocumentScrollViewer.