gpt4 book ai didi

wpf - 如何删除空段落所占用的空间?

转载 作者:行者123 更新时间:2023-12-04 11:36:33 24 4
gpt4 key购买 nike

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

我的代码如下:

<FlowDocument>
<Section>
<Paragraph>1</Paragraph>
<Paragraph>2</Paragraph>
<Paragraph></Paragraph>
<Paragraph>4</Paragraph>
</Section>
</FlowDocument>

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

enter image description here

编辑:这是一个更有意义的例子(根据评论):
<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>

这产生了完全相同的结果。

最佳答案

我犹豫要不要发布这个,因为我相信一定有更好的方法,但因为没有其他人回复......

一个流程文档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>



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

您可能会认为设置 LineHeight只有在空白段落上才有效,但 Section仍会尊重前一段的空格。由于前一段有正常 LineHeight ,您仍然可以获得 margin 。

因此,为了完全删除您的空白段落,您需要设置 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>



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

关于wpf - 如何删除空段落所占用的空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30876757/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com