8

I'm a beginer in C# Wpf and I want to make a flow document with few paragrah by programming. The problem is that there is a big space between pagraphs and i want to resize it to its minimum.

I found a solution by using a Xml statement, but i want to make it by programming :

<FlowDocument>
  <FlowDocument.Resources>
    <!-- This style is used to set the margins for all paragraphs in the FlowDocument to 0. -->
    <Style TargetType="{x:Type Paragraph}">
      <Setter Property="Margin" Value="0"/>
    </Style>
  </FlowDocument.Resources>

  <Paragraph>
    Spacing between paragraphs is caused by margins set on the paragraphs.  Two adjacent margins
    will "collapse" to the larger of the two margin widths, rather than doubling up.
  </Paragraph>

  <Paragraph>
    To eliminate extra spacing between two paragraphs, just set the paragraph margins to 0.
  </Paragraph>
</FlowDocument>

How can i do it ?.

thanx for you're help.

kmatyaszek
  • 19,016
  • 9
  • 60
  • 65
Walid A
  • 81
  • 1
  • 2

3 Answers3

5

Try this:

Style style = new Style(typeof(Paragraph));
style.Setters.Add(new Setter(Block.MarginProperty, new Thickness(0)));
myFlowDocument.Resources.Add(typeof(Paragraph), style);
kmatyaszek
  • 19,016
  • 9
  • 60
  • 65
  • 1
    This doesn't work for me nor does setting the Margin property in code (i.e. `paragraph.Margin = new Thickness(0)`). Nothing I do seems to want to reduce paragraph spacing. – Jordan May 05 '14 at 18:15
  • 1
    This is years after the fact but in Visual Studio 2017/ WPF this worked perfectly. It removes the extra space between paragraphs and still allows me to create bulleted lists onclick in the code. – noCodeMonkeys Apr 03 '17 at 14:09
3

No "programming" is necessary. The PagePadding property on FlowDocument worked for me:

<FlowDocument PagePadding="0">

MSDN definition for PagePadding:

Gets or sets a value that indicates the thickness of padding space between the boundaries of a page and the page's content.

Brett
  • 726
  • 9
  • 18
0

I tried above answers but this somewhat solved my problem.

I create the flowdocument object and attach it to the flowdocument viewer.

therefore i used this method. which described here but using code. How to: Adjust Spacing Between Paragraphs

       paragraph2.Margin = new Thickness(0);
       paragraph3.Margin = new Thickness(0);
Sanjuwa
  • 43
  • 1
  • 6