你如何删除空段落所占用的space?
How do you remove the space taken by an empty paragraph?
如何去掉段落中的 space?我试过使用负 margin/padding
但它不接受这些属性的负值。有什么想法吗?
我的代码如下:
<FlowDocument>
<Section>
<Paragraph>1</Paragraph>
<Paragraph>2</Paragraph>
<Paragraph></Paragraph>
<Paragraph>4</Paragraph>
</Section>
</FlowDocument>
并且,上述代码的输出如下:
编辑:这是一个更有意义的例子(根据评论):
<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>
结果完全相同。
我对 post 犹豫不决,因为我确信一定有更好的方法,但由于没有其他人回复....
流程文档 Section
似乎用与段落的 LineHeight
.
等同的空格来换行段落
LineHeight
不能为0,但可以很小。在 Section
上设置 LineHeight
将删除所有段落周围的空格。
<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
,你还是得到了保证金。
所以,为了完全删除你的空白段落,你需要在空白和前面的段落上设置 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,并且尝试使用它来检测空白段落是不可靠的,具体取决于填充段落的时间.
如果在您的场景中让 VM 属性指示段落是否为空是可行的,那么这将起作用:-
<FlowDocument>
<FlowDocument.Resources>
<Style TargetType="{x:Type Paragraph}">
<Setter Property="Margin" Value="0,0,0,18"/>
<Style.Triggers>
<Trigger Property="Tag" Value="True">
<Setter Property="Margin" Value="0"/>
<Setter Property="LineHeight" Value="0.1"/>
<Setter Property="LineStackingStrategy" Value="BlockLineHeight"/>
</Trigger>
</Style.Triggers>
</Style>
</FlowDocument.Resources>
<Section>
<Paragraph x:Name="p1" Tag="{Binding IsPara1Empty}">1</Paragraph>
<Paragraph x:Name="p2" Tag="{Binding IsPara2Empty}">2</Paragraph>
<Paragraph x:Name="p3" Tag="{Binding IsPara3Empty}"></Paragraph>
<Paragraph x:Name="p4" Tag="{Binding IsPara4Empty}">4</Paragraph>
</Section>
</FlowDocument>
根据您的字体大小,您可能需要使用样式的 "default" 边距值“0,0,0,18”。
或者,如果可以通过编程方式确定段落是否为空,您可以创建一个继承的 Paragraph
控件来公开 IsEmpty
依赖项 属性。触发器将使用它而不是 Tag
,并且您不需要 VM 属性。
试试这个
<FlowDocument>
<Section>
<Paragraph>
1
</Paragraph>
<Paragraph>
2
</Paragraph>
<Paragraph local:AttachNew.MyProperty="1">
</Paragraph>
<Paragraph>
4
</Paragraph>
</Section>
</FlowDocument>
public class AttachNew
{
public static int GetMyProperty(DependencyObject obj)
{
return (int)obj.GetValue(MyPropertyProperty);
}
public static void SetMyProperty(DependencyObject obj, int value)
{
obj.SetValue(MyPropertyProperty, value);
}
// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.RegisterAttached("MyProperty", typeof(int), typeof(AttachNew), new PropertyMetadata(0, new PropertyChangedCallback(ChangeProp)));
private static void ChangeProp(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Section objparent = (d as System.Windows.Documents.Paragraph).Parent as Section;
objparent.Blocks.Remove((d as System.Windows.Documents.Paragraph));
}
}
在 HTML 中,段落标记即使为空也会占用 space 的原因是因为它是块级元素,因此它 hasLayout。这意味着它具有渲染代理分配给它的填充和行高等属性,而且它会导致换行。
你能找到一种方法来检测段落是否为空并更改其显示规则吗?可见性不会删除元素,只会使其不可见,因此段落标签仍会导致讨厌的换行符。事实上,之前发布的行高解决方案仍然存在于换行符中。 (An article about display vs visibility.)
CSS 规则 display:none;
可用于将其从页面流中删除(参见上面的 hasLayout link)。我喜欢使用 CSS class 并以编程方式应用它。像这样的事情通常可以解决问题:
.hidden {display:none;height:0;width:0;}
您还可以在 class 中包含 line-height:0;
以涵盖之前的建议。
如何去掉段落中的 space?我试过使用负 margin/padding
但它不接受这些属性的负值。有什么想法吗?
我的代码如下:
<FlowDocument>
<Section>
<Paragraph>1</Paragraph>
<Paragraph>2</Paragraph>
<Paragraph></Paragraph>
<Paragraph>4</Paragraph>
</Section>
</FlowDocument>
并且,上述代码的输出如下:
编辑:这是一个更有意义的例子(根据评论):
<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>
结果完全相同。
我对 post 犹豫不决,因为我确信一定有更好的方法,但由于没有其他人回复....
流程文档 Section
似乎用与段落的 LineHeight
.
LineHeight
不能为0,但可以很小。在 Section
上设置 LineHeight
将删除所有段落周围的空格。
<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
,你还是得到了保证金。
所以,为了完全删除你的空白段落,你需要在空白和前面的段落上设置 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,并且尝试使用它来检测空白段落是不可靠的,具体取决于填充段落的时间.
如果在您的场景中让 VM 属性指示段落是否为空是可行的,那么这将起作用:-
<FlowDocument>
<FlowDocument.Resources>
<Style TargetType="{x:Type Paragraph}">
<Setter Property="Margin" Value="0,0,0,18"/>
<Style.Triggers>
<Trigger Property="Tag" Value="True">
<Setter Property="Margin" Value="0"/>
<Setter Property="LineHeight" Value="0.1"/>
<Setter Property="LineStackingStrategy" Value="BlockLineHeight"/>
</Trigger>
</Style.Triggers>
</Style>
</FlowDocument.Resources>
<Section>
<Paragraph x:Name="p1" Tag="{Binding IsPara1Empty}">1</Paragraph>
<Paragraph x:Name="p2" Tag="{Binding IsPara2Empty}">2</Paragraph>
<Paragraph x:Name="p3" Tag="{Binding IsPara3Empty}"></Paragraph>
<Paragraph x:Name="p4" Tag="{Binding IsPara4Empty}">4</Paragraph>
</Section>
</FlowDocument>
根据您的字体大小,您可能需要使用样式的 "default" 边距值“0,0,0,18”。
或者,如果可以通过编程方式确定段落是否为空,您可以创建一个继承的 Paragraph
控件来公开 IsEmpty
依赖项 属性。触发器将使用它而不是 Tag
,并且您不需要 VM 属性。
试试这个
<FlowDocument>
<Section>
<Paragraph>
1
</Paragraph>
<Paragraph>
2
</Paragraph>
<Paragraph local:AttachNew.MyProperty="1">
</Paragraph>
<Paragraph>
4
</Paragraph>
</Section>
</FlowDocument>
public class AttachNew
{
public static int GetMyProperty(DependencyObject obj)
{
return (int)obj.GetValue(MyPropertyProperty);
}
public static void SetMyProperty(DependencyObject obj, int value)
{
obj.SetValue(MyPropertyProperty, value);
}
// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.RegisterAttached("MyProperty", typeof(int), typeof(AttachNew), new PropertyMetadata(0, new PropertyChangedCallback(ChangeProp)));
private static void ChangeProp(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Section objparent = (d as System.Windows.Documents.Paragraph).Parent as Section;
objparent.Blocks.Remove((d as System.Windows.Documents.Paragraph));
}
}
在 HTML 中,段落标记即使为空也会占用 space 的原因是因为它是块级元素,因此它 hasLayout。这意味着它具有渲染代理分配给它的填充和行高等属性,而且它会导致换行。
你能找到一种方法来检测段落是否为空并更改其显示规则吗?可见性不会删除元素,只会使其不可见,因此段落标签仍会导致讨厌的换行符。事实上,之前发布的行高解决方案仍然存在于换行符中。 (An article about display vs visibility.)
CSS 规则 display:none;
可用于将其从页面流中删除(参见上面的 hasLayout link)。我喜欢使用 CSS class 并以编程方式应用它。像这样的事情通常可以解决问题:
.hidden {display:none;height:0;width:0;}
您还可以在 class 中包含 line-height:0;
以涵盖之前的建议。