WPF Multiline TextBlock LineBreak问题

时间:2011-01-08 08:36:10

标签: wpf textblock

我有以下代码

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:

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

然后它正确地产生结果。为什么我无法将内嵌文字添加到Textblock并要求我使用Run

1 个答案:

答案 0 :(得分:8)

请参阅此答案: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");
相关问题