在 WPF 文本块中获取段落的最佳方法是什么?(换行符?)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1559754/
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
What the best way to get paragraphs in a WPF textblock? (newline chars?)
提问by MrGreggles
I have some text which has "\r\n" newline markers. I would like to have the newlines in a WPF textblock. I've tried replacing "\r\n" with "& # 13;" (without the spaces), which worked when I set the Text property in XAML, but doesn't seem to work when setting from the C# code-behind.
我有一些带有 "\r\n" 换行标记的文本。我想在 WPF 文本块中有换行符。我试过用“& # 13;”替换“\r\n” (没有空格),当我在 XAML 中设置 Text 属性时有效,但在从 C# 代码隐藏设置时似乎不起作用。
So...what's the standard way to convert "\r\n" to newlines in a WPF textblock?
那么......在 WPF 文本块中将 "\r\n" 转换为换行符的标准方法是什么?
回答by Ash
Try these for a more WPF centric solution.
尝试这些以获得更以 WPF 为中心的解决方案。
TextBlock.Inlines.Add(new Run("First"));
TextBlock.Inlines.Add(new LineBreak());
TextBlock.Inlines.Add(new Run("Second"));
See also : XAML based Answer
另请参阅:基于 XAML 的答案
回答by Kent Boogaart
textBlock.Text = string.Format("One{0}Two", Environment.NewLine);
回答by ChrisBD
When writing C# I always use "System.Environment.Newline" for the newline carriage return.
在编写 C# 时,我总是使用“System.Environment.Newline”作为换行符回车。
It means that you don't have to worry about character coding or what destination OS uses.
这意味着您不必担心字符编码或目标操作系统使用什么。
I have also found it to work on WPF GUI when called from the underlying .cs file.
我还发现它在从底层 .cs 文件调用时可以在 WPF GUI 上工作。