How to remove space taken by an empty paragraph?

How to get rid of space from a paragraph? I tried using negative margin/padding , but it does not accept negative values ​​for these properties. Any idea?

My code is below:

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

And the outputs for the above code are given below:

enter image description here

EDIT: Here is an example that will make more sense (according to the comments):

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

Which makes the same result.

+7
wpf xaml paragraph
source share
4 answers

I am shy about posting this since I'm sure there should be a better way, but since no one answered ...

The Section stream document appears to contain paragraphs with a space equivalent to the LineHeight paragraph.

LineHeight cannot be 0, but can be very small. Setting LineHeight to Section will remove spaces around ALL paragraphs.

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

Setting LineHeight like this usually does not affect the text inside the paragraphs, because by default LineStackingStrategy uses font height instead. Note that the empty paragraph still has a height.

You might think that setting LineHeight only in an empty paragraph would work, but Section would still respect the spaces of the previous paragraph. Since the previous paragraph has a normal LineHeight , you still get the difference.

So, in order to completely remove the empty paragraph, you need to set LineHeight to the space and the previous paragraph, and your empty paragraph to use LineHeight as its block height:

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

I tried to write a trigger that would do this automatically for empty paragraphs, but unfortunately Paragraph.Inlines.Count not a DependencyProperty, and trying to use it to detect empty paragraphs is unreliable depending on when the paragraph is filled.

+4
source share

If your script might have VM properties indicating whether the paragraphs are empty, this will work: -

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

Depending on your font size, you may need to play with the default value of 0.0.0.18 by default.

Alternatively, if you can programmatically determine whether a paragraph is empty, you can create an inherited Paragraph control that exposes the IsEmpty dependency property. The trigger will use this instead of Tag , and you will not need the properties of the virtual machine.

+2
source share

try it

 <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)); } } 
+1
source share

In HTML, the reason that a paragraph tag, even when empty, occupies a space, is because it is a block level element, and therefore it hasLayout . This means that it has properties, such as padding and line height, assigned to it by the rendering agent, and also that it will cause line breaks .

Can you find a way to determine if a paragraph will be blank and change its display rule? Visibility does not remove the item, it only makes it invisible, so the paragraph tag will still produce a broken line. In fact, this line-height solution posted earlier still remains in line break. ( Article on visibility and visibility .

CSS rule display:none; can be used to remove it from the page stream (see hasLayout link above). I like to use the CSS class and apply it programmatically. Something like this usually does the trick:

 .hidden {display:none;height:0;width:0;} 

You can also enable line-height:0; in this class to cover the previous sentence.

+1
source share

All Articles