开发者

Adding text in a new line in WPF RichTextBox at runtime

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.Inli开发者_JS百科nes.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?


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>


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜