Adding text in a new line in WPF RichTextBox at ru

2019-04-25 02:28发布

I want to add some text in a WPF RichTextBox at runtime in a new line. I can do this using:

FlowDocument mcFlowDoc = new FlowDocument();
mcFlowDoc = richTextBox.Document;
Paragraph pr = new Paragraph();
pr.Inlines.Add(status);
mcFlowDoc.Blocks.Add(pr);
StatusText.Document = mcFlowDoc;

But there is too much of a gap between two lines. How can I fix this?

3条回答
forever°为你锁心
2楼-- · 2019-04-25 02:48

Try pr.Margin = new Thickness(0.0) to remove the gaps between paragraphs.

查看更多
Emotional °昔
3楼-- · 2019-04-25 02:57

According to the documentation, Paragraph spacing is defined by margins, which do not accumulate (no doubling up), so Julien Lebosquain's answer is correct.

MSDN on FlowDocument Paragraph Spacing

查看更多
该账号已被封号
4楼-- · 2019-04-25 03:02

To avoid having to manually set the margin for every paragraph, you can add this to the RichTextBox's XAML:

<RichTextBox>
  <RichTextBox.Resources>
    <Style TargetType="{x:Type Paragraph}">
      <Setter Property="Margin" Value="0"/>
    </Style>
  </RichTextBox.Resources>
</RichTextBox>
查看更多
登录 后发表回答