如何将新行放入 wpf TextBlock 控件中?
我正在从 XML 文件中获取文本,并且想插入一些由文本块渲染解释为新行的新行。
我已经尝试过:
<data>Foo bar baz \n baz bar</data>
但数据仍然显示,没有新行。我通过 C# 通过 .Text
属性设置 的内容。
我需要在 XML 中添加什么内容才能在 GUI 中呈现新行?
我已经尝试过类似的方法,在 XAML 中手动设置文本:
<TextBlock Margin="0 15 0 0" Width="600">
There
is a new line.
</TextBlock>
虽然编码的字符没有出现在 GUI 中,但它也没有给我一个新行。
I'm fetching text from an XML file, and I'd like to insert some new lines that are interpreted by the textblock render as new lines.
I've tried:
<data>Foo bar baz \n baz bar</data>
But the data is still displayed without the new line. I set the contents of <data>
via the .Text
property via C#.
What do I need to put in the XML in order for it to render the new line in the GUI?
I've tried something like this manually setting the text in the XAML:
<TextBlock Margin="0 15 0 0" Width="600">
There
is a new line.
</TextBlock>
And while the encoded character doesn't appear in the GUI it also doesn't give me a new line.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
您可以尝试在数据中添加新行:
如果这不起作用,您可能需要手动解析字符串。
顺便说一下,如果您需要直接 XAML,那很简单:
You can try putting a new line in the data:
If that does not work you might need to parse the string manually.
If you need direct XAML that's easy by the way:
为了完整起见:您还可以这样做:
x0A 是转义的十六进制换行符。相当于\n
For completeness: You can also do this:
x0A is the escaped hexadecimal Line Feed. The equivalent of \n
您必须使用
Or 如果您愿意:
请注意:如果您都使用 &10 并且转到文本中的下一行,您将有两个空行。
详细信息请参见此处:
http://msdn.microsoft.com/en-us/library/ms788746.aspx
you must use
Or if you prefer :
watch out : if you both use &10 AND you go to the next line in your text, you'll have TWO empty lines.
here for details :
http://msdn.microsoft.com/en-us/library/ms788746.aspx
您还可以使用绑定
并像这样设置 MyText:
You can also use binding
And set MyText like this:
尽管这是一个老问题,但我刚刚遇到这个问题并以与给定答案不同的方式解决它。也许对其他人有帮助。
我注意到,尽管我的 XML 文件看起来像:
当它被读入我的 C# 代码时,该字符串有双反斜杠。
为了解决这个问题,我编写了一个 ValueConverter 来去除多余的反斜杠。
Even though this is an old question, I've just come across the problem and solved it differently from the given answers. Maybe it could be helpful to others.
I noticed that even though my XML files looked like:
When it was read into my C# code the string had double backslashes.
To fix this I wrote a ValueConverter to strip the extra backslash.
如果其他方法都失败了,你也可以使用
If all else fails you can also use
使用 System.Environment.NewLine 是唯一对我有用的解决方案。
当我尝试 \r\n 时,它只是在文本框中重复实际的 \r\n 。
Using System.Environment.NewLine is the only solution that worked for me.
When I tried \r\n, it just repeated the actual \r\n in the text box.
只是根据@Ketobomb的建议,你可以按照你的风格使用它吗:
并以这种方式使用它:
just from the @Ketobomb suggestion, could you use this in your style:
and use it in this way:
在 RichTextBox“rtb”中插入“换行符”或“段落符”,如下所示:
获取 NewLine 项的唯一方法是首先插入带有“\r\n”项的文本,然后应用更多有效的代码在 Selection 和/或 TextRange 对象上。这可确保 \par 项目转换为 \line 项目、根据需要保存,并且在重新打开 *.Rtf 文件时仍然正确。这是我经过努力尝试后发现的。我的三个代码行需要被更多代码(带有循环)包围,以将 TextPointer 项(.Start .End .ContentStart .ContentEnd)设置为行和中断应该去的位置,我已经成功地完成了我的目的。
Insert a "line break" or a "paragraph break" in a RichTextBox "rtb" like this:
The only way to get the NewLine items is by inserting text with "\r\n" items first, and then applying more code which works on Selection and/or TextRange objects. This makes sure that the \par items are converted to \line items, are saved as desired, and are still correct when reopening the *.Rtf file. That is what I found so far after hard tries. My three code lines need to be surrounded by more code (with loops) to set the TextPointer items (.Start .End .ContentStart .ContentEnd) where the Lines and Breaks should go, which I have done with success for my purposes.
定义一个属性,在设置时将 \n 替换为 System.Environment.Newline:
将文本框绑定到该属性:
读取 xml 数据并将其分配给属性
Any \n xml 数据中的数据在 TextBox 中显示为新行。
Define a property which replaces \n with System.Environment.Newline on set:
Bind your textbox to the property:
Read the xml data and assign it to the property
Any \n in the xml data is displayed as a new line in the TextBox.
如果您要绑定内容,这非常有效:
In case you are binding the content, this works very well:
您必须确保文本块启用了此选项:
You have to ensure that the textblock has this option enabled: