WPF 文本块,文本属性中的换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/837367/
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
WPF Textblock, linebreak in Text attribute
提问by ScottCate
Is there a way to have \n
make a line break in a TextBlock
?
有没有办法在\n
a 中换行TextBlock
?
<TextBlock Text="line1\nLine2" />
Or is there a better way to force a middle line break, inside the Text
attribute?
或者有没有更好的方法来强制在Text
属性内换行?
<LineBreak />
This doesn't work for me, it needs to be the value of the Text
attribute, because the text string is being set from an outside source.
这对我不起作用,它需要是Text
属性的值,因为文本字符串是从外部源设置的。
I'm familiar with LineBreak
but it's not the answer I'm looking for.
我很熟悉,LineBreak
但这不是我正在寻找的答案。
回答by Paul Alexander
Try this:
尝试这个:
<TextBlock>
line1
<LineBreak />
line2
</TextBlock>
回答by Noaki
I know this is ressurecting an old question, but I had the same problem. The solution for me was to use HTML encoded line feeds (&#10;
).
我知道这是在回答一个老问题,但我遇到了同样的问题。我的解决方案是使用 HTML 编码的换行符 ( &#10;
)。
Line1&#10;Line2
Looks like
好像
Line1
Line2
1号线
2号线
For more of the HTML encoded characters check out w3schools
有关更多 HTML 编码字符,请查看w3schools
回答by Stephane Halimi
The easiest way is
最简单的方法是
<TextBlock> blabla <LineBreak /> coucou <LineBreak /> coucou 2 </TextBlock>
<TextBlock> blabla <LineBreak /> coucou <LineBreak /> coucou 2 </TextBlock>
So you just write XAML code, and the <LineBreak />
has exactly the same meaning the
in HTML or the "\n" in C#.
因此,您只需编写 XAML 代码,并且HTML 中的 或 C# 中的“\n”<LineBreak />
具有完全相同的含义
。
回答by jcollum
回答by Cameron MacFarland
How about breaking the line into two tags?
把这条线分成两个标签怎么样?
<StackPanel>
<TextBlock Text="Line1" />
<TextBlock Text="Line2" />
</StackPanel>
回答by radu florescu
Correct way to use it may be the following :
正确的使用方法可能如下:
<TextBlock>
<Span>text1</Span>
<LineBreak/>
<Span>text2</Span>
</TextBlock>
回答by user2063329
<LineBreak/> will not work if it is inside a collection such as Grid or StackPanel. In such cases the following would work as shown:
如果 <LineBreak/> 在诸如 Grid 或 StackPanel 之类的集合中,则它不起作用。在这种情况下,如下所示:
回答by newman
If you are binding TextBlock's Text, none of the other answers work. Simply add '\n' to the binding text to where you want to break.
如果您要绑定 TextBlock 的文本,则其他答案均无效。只需将 '\n' 添加到要中断的绑定文本中。
回答by user1551704
<HyperlinkButton
Content="Apply and restart this pplication! Note that modifying these settings requires the application to be restarted." />
CRLF simple way = !
CRLF 简单的方法 = !
!
- Work on all wpf, xaml, silverlight controls like TextBlock, HyperlinkText and more
!
- 处理所有 wpf、xaml、silverlight 控件,如 TextBlock、HyperlinkText 等
回答by MelloG
just use the AccessText control. you can use it like a label and you have the property TextWrapping="WrapWithOverflow"
只需使用 AccessText 控件。你可以像标签一样使用它,你有属性 TextWrapping="WrapWithOverflow"
eg.
例如。
Mine is like that and it's working fine. Also, you don't have any problems on changing the text dinamically.
我的就是这样,而且工作正常。此外,您在动态更改文本方面没有任何问题。