如何:调整段落间的间距

此示例演示如何调整或消除流内容中段落之间的间距。

在流内容中,显示在段落之间的额外空间是由于在这些段落上设置的边距所造成的;因此,可以通过调整这些段落上的边距来控制段落之间的间距。 若要完全消除两个段落之间的额外间距,请将段落的边距设置为 0。 若要在整个 FlowDocument 内实现段落之间的统一间距,请使用样式来为 FlowDocument 中的所有段落设置统一的边距值。

应该注意的是,两个相邻段落之间的边距将“折算”成两个边距值中较大的一个,而不是将两值相加。 因此,如果两个相邻段落的边距分别为 20 像素和 40 像素,那么两个段落之间的最终间距为 40 像素,即两个边距值中较大的一个。

示例

下面的示例使用样式将 FlowDocument 中的所有 Paragraph 元素的边距设置为 0,这将有效地消除 FlowDocument 中段落之间的额外间距。

<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>