我有以下代码

txtBlock1.Inlines.Add("This is first paragraph \n This is second paragraph");


然后TextBlock将显示:

This is first paragraph
This is second paragraph


但是,如果我有以下内容(虽然我是等效的);

txtBlock1.Inlines.Add("This is first paragraph");
txtBlock1.Inlines.Add("\n");
txtBlock1.Inlines.Add("This is second paragraph");


TextBlock显示:

This is first paragraph // but second paragraph missing


如果我分开换行符,则换行符后的其余文本不会显示。为什么?

我必须使用运行:

Run run1 = new Run();
run1.Text = "First Paragraph";
run1.Text += "\n";
run1.Text += "Second Paragraph";
txtBlock1.Inlines.Add(run1);


然后,它会正确产生结果。为什么不能将内联文本添加到Textblock并要求我使用Run

最佳答案

查看此答案:What the best way to get paragraphs in a WPF textblock? (newline chars?)

你需要:

txtBlock1.Inlines.Add("This is first paragraph");
txtBlock1.Inlines.Add(new LineBreak());
txtBlock1.Inlines.Add("This is second paragraph");

关于wpf - WPF多行TextBlock LineBreak问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4633116/

10-13 06:55