How do I change RichTextBox paragraph spacing?

WpfRichtextbox

Wpf Problem Overview


I am using a RichTextBox in WPF, and am trying to set the default paragraph spacing to 0 (so that there is no paragraph spacing). While I could do this in XAML, I would like to achieve it programmatically if possible. Any ideas?

Wpf Solutions


Solution 1 - Wpf

I did it with style (pun indented)

<RichTextBox  Margin="0,51,0,0" Name="mainTextBox" >
        <RichTextBox.Resources>
            <Style TargetType="{x:Type Paragraph}">
                <Setter Property="Margin" Value="0"/>
            </Style>
        </RichTextBox.Resources>
    </RichTextBox>

Solution 2 - Wpf

Using Line Height

RichTextBox rtb = new RichTextBox();    
Paragraph p = rtb.Document.Blocks.FirstBlock as Paragraph;    
p.LineHeight = 10;

Solution 3 - Wpf

Close, so you got the points. Actually it turned out to be setting the margin,

p.Margin = new Thickness(0);

Solution 4 - Wpf

For me on VS2017 in WPF works this:

 <RichTextBox HorizontalAlignment="Left" Height="126" Margin="10,280,0,0" VerticalAlignment="Top" Width="343" FontSize="14" Block.LineHeight="2"/>

The key is Block.LineHeight="2"

You can found this also in Properties view but you can't change below 6px from there.

Solution 5 - Wpf

RichTextBox rtb = new RichTextBox();
rtb.SetValue(Paragraph.LineHeightProperty, 1.0);

Solution 6 - Wpf

<RichTextBox  Height="250" Width="500" VerticalScrollBarVisibility="Auto" TextWrapping="Wrap" IsReadOnly="True" >
    <Paragraph>
        XYZ
        <LineBreak />
    </Paragraph>
</RichTextBox>

Solution 7 - Wpf

In C# 2008 WAP

richtextbox1.SelectionCharOffset =
    -1 * ( Convert.ToInt32(R223.Txt_Space_Before.Text) * 100);

or

richtextbox1.SelectionCharOffset =
    Convert.ToInt32(R223.Txt_Space_Before.Text) * 100;

can be used for Line Spacing.

This is the only way you can have line height spacing.

Solution 8 - Wpf

I know this question was posted before I even started coding but I found that simply setting ShowSelectedMargin to true did the trick

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDarren OsterView Question on Stackoverflow
Solution 1 - WpfmoogsView Answer on Stackoverflow
Solution 2 - WpfRamesh SoniView Answer on Stackoverflow
Solution 3 - WpfDarren OsterView Answer on Stackoverflow
Solution 4 - Wpfm4rcelView Answer on Stackoverflow
Solution 5 - WpfsenquevilaView Answer on Stackoverflow
Solution 6 - WpfHaasan SachdevView Answer on Stackoverflow
Solution 7 - WpfDannyView Answer on Stackoverflow
Solution 8 - WpfRRMView Answer on Stackoverflow