8

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?

Zach Johnson
  • 23,678
  • 6
  • 69
  • 86
Jeevan Bhatt
  • 5,881
  • 18
  • 54
  • 82
  • BTW, there's no point of creating a new `FlowDocument` (`mcFlowDoc = new FlowDocument();`) when you're changing its reference the next line `mcFlowDoc = richTextBox.Document;` – Alireza Noori Nov 17 '12 at 11:25

3 Answers3

9

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>
Merad
  • 749
  • 1
  • 6
  • 14
  • can you give an example of how you setup xaml for a Rich Text Box block or document ? (I'm just getting started with Rich Text Box programming from c#) – dr d b karron Mar 29 '14 at 16:51
7

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

Julien Lebosquain
  • 40,639
  • 8
  • 105
  • 117
3

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

Ian Gilham
  • 1,916
  • 3
  • 20
  • 31