【问题标题】:WPF Multiline TextBlock LineBreak issuesWPF Multiline TextBlock LineBreak 问题
【发布时间】:2011-01-08 18:55:50
【问题描述】:

我有以下代码

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");

文本块显示:

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

【问题讨论】:

  • omg...这实际上是 VS2010 Ultimate 中的一个错误。 Sometext 在我调整窗口大小之前不会显示...如果有人碰巧遇到这个,请调整窗口大小

标签: wpf textblock


【解决方案1】:

看到这个答案: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");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-23
    • 2015-07-01
    • 2011-05-04
    • 2015-09-28
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多