WPF TextBlock 在多行上显示字符串
我有一个字符串:
Item A\r\nItem B\r\nItem C
如何将此字符串绑定到 TextBlock 以便它显示为:
Item A
Item B
Item C
谢谢
I have a string:
Item A\r\nItem B\r\nItem C
How can I bind this string to a TextBlock so that it appears as:
Item A
Item B
Item C
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需使
TextBlock
足够大以显示三行即可。如果TextBlock
在Text
中发现换行符和回车符,则可以对文本进行换行。编辑:此外,请确保换行符和回车符不是硬编码的。我的意思是,这两者之间存在差异:
并且...
第二个字符串将在
TextBlock
中正确显示,但第一个字符串将仅显示在单行中,如“Item A\r \nItem B\r\nItem C" 因为换行符和回车符是硬编码的而不是转义字符。您可以通过用转义序列替换硬编码的换行符和回车符来解决此问题,方法是:
或最好是:
Just make the
TextBlock
big enough to show three lines.TextBlock
is capable of wrapping the text if it finds newline and carriage return inText
.EDIT: Also, make sure that the newline and carriage returns are not hard coded. What I mean is that there is a difference between these two:
and...
The second string will display correctly in the
TextBlock
but the first will just get displayed in a single line as "Item A\r\nItem B\r\nItem C" because the newline and carriage characters are hard coded instead of being escape characters.You can fix that by replacing the hard coded newline and carriage characters with their escape sequences, by:
or preferably by: