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
?
I have the following code
txtBlock1.Inlines.Add("This is first paragraph \n This is second paragraph");
then TextBlock would display:
This is first paragraph
This is second paragraph
But, if I have the following (which I though is equivalent);
txtBlock1.Inlines.Add("This is first paragraph");
txtBlock1.Inlines.Add("\n");
txtBlock1.Inlines.Add("This is second paragraph");
TextBlock display:
This is first paragraph // but second paragraph missing
If I separate out the linebreak then the rest of text after the linebreak doesn't show. Why?
I have to use run:
Run run1 = new Run();
run1.Text = "First Paragraph";
run1.Text += "\n";
run1.Text += "Second Paragraph";
txtBlock1.Inlines.Add(run1);
Then it produce the result correctly. Why I cannot add inline text to Textblock
and require me to use Run
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅此答案:什么在 WPF 文本块中获取段落的最佳方法? (换行符?)
您需要:
See this answer: What the best way to get paragraphs in a WPF textblock? (newline chars?)
You need: