Aaron Thomas
Aaron Thomas

Reputation: 5281

WPF flowdocument block padding

What is the best way to adjust the distance between blocks in a flowdocument?

I'm adding blocks to a flowdocument in code-behind. Assuming flowDoc is an instance of FlowDocument that is ready to go, I add a block with:

Paragraph para = new Paragraph();
para.Inlines.Add("Hello there");
flowDoc.Blocks.Add(para);

If I run this multiple times, it adds a new block each time. How do I adjust the distance between blocks?

Changing the Paragraph.Padding property seems to change padding for items within the Paragraph, not between Paragraphs in the Block.

Upvotes: 1

Views: 1488

Answers (1)

Aaron Thomas
Aaron Thomas

Reputation: 5281

So, after digging around some more, I realized I need to use the Margin property of Paragraph, not the Padding property of Blocks. Let me explain.

In the WPF guide on MSDN, there is a section titled "Alignment, Margins, and Padding Overview" under the "Layout" section. It states:

The Margin property describes the distance between an element and its child or peers. [emphasis added]

So, instead of thinking about how to adjust spacing between child elements using Padding, I can adjust spacing between peers using Margin. Here's how it would look in code-behind:

Paragraph para = new Paragraph();
para.Inlines.Add("Hello there");
// adjust spacing between paragraphs with Margin property
para.Margin = new System.Windows.Thickness(5, 1, 1, 5);
flowDoc.Blocks.Add(para);

...and, here's how to adjust margin in XAML:

<FlowDocument>
    <Paragraph Name="newPara" Margin="5,1,1,5">
        Hello There
    </Paragraph>
</FlowDocument>

Upvotes: 2

Related Questions