wpf 如何在没有 XAML 的情况下在 TextBlock 中插入换行符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24130980/
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
How can I insert a newline into a TextBlock without XAML?
提问by allquixotic
I have created a WPF TextBlockinside a Labelin code(XAML is not possible in my case) as follows:
我创建了一个WPFTextBlock内Label的代码(XAML是不可能在我的情况),如下所示:
Label l = new Label();
TextBlock tb = new TextBlock();
l.Content = tb;
I then come to a situation where I need to set the .Textproperty of TextBlockcontaining a new line, such as:
然后我遇到了需要设置包含新行的.Text属性的情况TextBlock,例如:
tb.Text = "Hello\nWould you please just work?";
I have tried numerous encodings (HTML encoding, ASCII encoding, etc.) of various newline combinations (carriage return, linefeed, carriage return plus linefeed, linefeed plus carriage return, double linefeed, double carriage return, etc... ad nauseum).
我尝试了各种换行符组合(回车、换行、回车加换行、换行加回车、双换行、双回车等)的多种编码(HTML 编码、ASCII 编码等),令人作呕。
- Answers should contain absolutely no XAML.
- Answers should assume that the original objects were created using C# code and notXAML.
- Answers should not refer to bindingof WPF properties. No binding is being used. The "Text" property of the "TextBlock" object is being set. The newline mustbe inserted there.
- 答案应该绝对不包含XAML。
- 答案应该假设原始对象是使用 C# 代码而不是XAML 创建的。
- 答案不应涉及WPF 属性的绑定。没有使用绑定。正在设置“TextBlock”对象的“Text”属性。必须在那里插入换行符。
If this is impossible, please let me know how I can programmatically add newlines by dynamically replacing each newline in an arbitrary input Stringinto a LineBreakobject (or whatever is needed to get this working). The input Stringwill have arbitrary (readable text) contents that I am unable to anticipate in advance because the text itself is dynamic (user-defined); the source strings will have the linefeed character (aka LF, aka \n) but I can easily replace that with whatever is needed.
如果这是不可能的,请告诉我如何通过将任意输入中的每个换行符动态替换String为LineBreak对象(或使其工作所需的任何内容)以编程方式添加换行符。输入String将包含我无法提前预测的任意(可读文本)内容,因为文本本身是动态的(用户定义的);源字符串将具有换行符(又名 LF,又名\n),但我可以轻松地将其替换为所需的任何内容。
Also, if it is easier to do this with a Labeldirectly rather than a TextBlockin a Label, that is good too -- I can use that. I just need a control with automatic plain text line wrapping.
另外,如果是更容易被做到这一点Label直接,而不是TextBlock在一个Label,那也不错-我可以使用。我只需要一个带有自动纯文本换行功能的控件。
回答by metacubed
You have a few choices:
你有几个选择:
Use the Environment.NewLineproperty:
使用Environment.NewLine属性:
TextBlock tb = new TextBlock();
tb.Text = "Hello" + Environment.NewLine + "Would you please just work?";
Or, manually add Runs and LineBreaks to the TextBlock:
或者,手动将Runs 和LineBreaks添加到TextBlock:
TextBlock tb = new TextBlock();
tb.Inlines.Add(new Run("Hello"));
tb.Inlines.Add(new LineBreak());
tb.Inlines.Add(new Run("Would you please just work?"));
回答by okieh
Just another small note. I had a similar problem and took the string from a resource file. What I noticed is that .NET delivered the string "\r\n" as "\\r\\n". When I corrected that by replacing the double-backslashes with normal backslashes everything worked as expected.
只是另一个小笔记。我遇到了类似的问题,并从资源文件中获取了字符串。我注意到的是,.NET 将字符串“\r\n”作为“\\r\\n”传递。当我通过用普通反斜杠替换双反斜杠来纠正它时,一切都按预期工作。

