WPF 文本块,文本属性中的换行符
有没有办法让 \n
在 TextBlock
中换行?
<TextBlock Text="line1\nLine2" />
或者是否有更好的方法在 Text
属性内强制中间换行?
<LineBreak />
这对我不起作用,它需要是 Text
属性的值,因为文本字符串是从外部源设置的。
我熟悉 LineBreak
但这不是我正在寻找的答案。
Is there a way to have \n
make a line break in a TextBlock
?
<TextBlock Text="line1\nLine2" />
Or is there a better way to force a middle line break, inside the Text
attribute?
<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.
I'm familiar with LineBreak
but it's not the answer I'm looking for.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
尝试这个:
Try this:
我知道这又重新提出了一个老问题,但我也遇到了同样的问题。 我的解决方案是使用 HTML 编码的换行符 (
)。
好像
有关更多 HTML 编码字符,请查看 w3schools
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 (
).Looks like
For more of the HTML encoded characters check out w3schools
最简单的方法是
所以你只需编写 XAML 代码,
的含义与 HTML 中的或“\n”完全相同在 C# 中。
The easiest way is
<TextBlock> blabla <LineBreak /> coucou <LineBreak /> coucou 2 </TextBlock>
So you just write XAML code, and the
<LineBreak />
has exactly the same meaning thein HTML or the "\n" in C#.
http://www. longhorncorner.com/UploadFile/mahesh/XamlLineBreak06092005152257PM/XamlLineBreak.aspx
<LineBreak/>
http://www.longhorncorner.com/UploadFile/mahesh/XamlLineBreak06092005152257PM/XamlLineBreak.aspx
将这一行分成两个标签怎么样?
How about breaking the line into two tags?
<换行符> 如果它位于 Grid 或 StackPanel 等集合中,则不起作用。
在这种情况下,以下内容将按所示工作:
<LineBreak/> will not work if it is inside a collection such as Grid or StackPanel.
In such cases the following would work as shown:
正确的使用方法可能如下:
Correct way to use it may be the following :
对于同一文本块中的多行,最适合我的方法是:
确保不使用 TextWrapping="Wrap"。 使用 TextWrapping="NoWrap" 或不使用任何内容。
The Best way that worked for me for multiple lines in the same Textblock is:
Make sure to not use TextWrapping="Wrap". Use TextWrapping="NoWrap" or use nothing.
CRLF simple way =
!
!
- 适用于所有 wpf、xaml、silverlight 控件如 TextBlock、HyperlinkText 等CRLF simple way =
!
!
- Work on all wpf, xaml, silverlight controls like TextBlock, HyperlinkText and more如果您要绑定 TextBlock 的 Text,则其他答案都不起作用。 只需将“\n”添加到要中断的绑定文本中即可。
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.
当我使用绑定时,这个
对我不起作用。 但这有效:
this
did not work for me, when I used binding. But this works:这也可以正常工作:
This also works fine:
我参加聚会迟到了但是..
这或多或少是我的做法,(注意我的 ItemSources 是纯字符串,没有格式化,而且我不需要“convertBack”任何东西)
I'm late to the party but ..
this is more or less how I did it ,(mind my ItemSources are plain strings, not formatted , and I didn't need to 'convertBack' anything)
我遇到了类似的问题,想要将 xaml 标记字符串绑定到 TextBlock。 本质上是将声明性标记存储在 TextBlock 内的字符串中以供以后使用。
这就是我的做法:我对 TextBlock 进行了子类化,以使 InlineCollection 可绑定,并在字符串和 InlineCollection(或者实际上是内联的通用列表)之间编写了一个转换器。
I was having a similar problem and wanted to bind a String of xaml markup to a TextBlock. Essentialy storing the declarative markup inside a TextBlock in a string for later use.
This is how I did: I subclassed the TextBlock to make the InlineCollection bindable and wrote a Converter between the string and an InlineCollection(or actually a generic list of Inlines.)
只需使用 AccessText 控件即可。 您可以像标签一样使用它,并且您拥有属性 TextWrapping="WrapWithOverflow"
例如。
我的就是这样并且工作正常。 此外,动态更改文本也没有任何问题。
just use the AccessText control. you can use it like a label and you have the property TextWrapping="WrapWithOverflow"
eg.
Mine is like that and it's working fine. Also, you don't have any problems on changing the text dinamically.
这也很好用。
使用此方法,我们可以根据需要修改每一行中的文本属性。
This also works fine.
Using this method we can modify the text properties in each line as we required.