你如何删除空段所占用的空间?

Max*_*ard 7 wpf xaml paragraph

如何摆脱段落的空间?我尝试使用否定margin/padding但它不接受这些属性的负值.任何的想法?

我的代码如下:

<FlowDocument>
    <Section>
        <Paragraph>1</Paragraph>
        <Paragraph>2</Paragraph>
        <Paragraph></Paragraph>
        <Paragraph>4</Paragraph>
    </Section>
</FlowDocument>
Run Code Online (Sandbox Code Playgroud)

并且,上面代码的输出如下:

在此输入图像描述

编辑:这是一个更有意义的例子(根据评论):

<FlowDocument>
    <Section>
        <Paragraph>
            <TextBlock Text="1" Visibility="Visible"/>
        </Paragraph>
        <Paragraph>
            <TextBlock Text="2" Visibility="Visible"/>
        </Paragraph>
        <Paragraph>
            <TextBlock Text="3" Visibility="Collapsed"/>
        </Paragraph>
        <Paragraph>
            <TextBlock Text="4" Visibility="Visible"/>
        </Paragraph>
    </Section>
</FlowDocument>
Run Code Online (Sandbox Code Playgroud)

这使得完全相同的结果.

Gaz*_*yer 5

我犹豫是否要发布此内容,因为我确信一定有更好的方法,但由于没有其他人回复......

流文档Section似乎用与段落的LineHeight.

LineHeight不能为 0,但可以非常小。设置将删除所有段落周围LineHeightSection空白。

<FlowDocumentScrollViewer>
    <FlowDocumentScrollViewer.Resources>
        <Style TargetType="Paragraph">
            <Setter Property="Background" Value="LightBlue" />
        </Style>
    </FlowDocumentScrollViewer.Resources>

    <FlowDocument>
        <Section LineHeight="0.1">
            <Paragraph>1</Paragraph>
            <Paragraph>2</Paragraph>
            <Paragraph/>                       
            <Paragraph>4</Paragraph>
            <Paragraph>5</Paragraph>
        </Section>
    </FlowDocument>

</FlowDocumentScrollViewer>
Run Code Online (Sandbox Code Playgroud)

这样的设置LineHeight一般不会影响段落内的文本,因为默认LineStackingStrategy使用字体的高度。请注意空白段落仍然具有高度。

您可能认为LineHeight仅在空白段落上设置会起作用,但Section仍会尊重前一段的空白。由于前一段有正常LineHeight,你仍然可以获得保证金。

因此,为了完全删除空白段落,您需要设置LineHeight空白和前一个段落,并告诉您的空白段落使用LineHeight作为其块高度:

<FlowDocumentScrollViewer>
    <FlowDocumentScrollViewer.Resources>
        <Style TargetType="Paragraph">
            <Setter Property="Background" Value="LightBlue" />
        </Style>
    </FlowDocumentScrollViewer.Resources>

    <FlowDocument>
        <Section>
            <Paragraph>1</Paragraph>
            <Paragraph LineHeight="0.1">2</Paragraph>
            <Paragraph LineHeight="0.1" LineStackingStrategy="BlockLineHeight"/>                       
            <Paragraph>4</Paragraph>
            <Paragraph>5</Paragraph>
        </Section>
    </FlowDocument>

</FlowDocumentScrollViewer>
Run Code Online (Sandbox Code Playgroud)

我尝试编写一个触发器,可以自动对空白段落执行此操作,但不幸的Paragraph.Inlines.Count是它不是 DependencyProperty,并且尝试使用它来检测空白段落是不可靠的,具体取决于段落何时填充。