views:

3850

answers:

6

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?

+2  A: 

Using Line Height

RichTextBox rtb = new RichTextBox();    
Paragraph p = rtb.Document.Blocks.FirstBlock as Paragraph;    
p.LineHeight = 10;
Ramesh Soni
This is not enough if they can change font size.
Donnelle
+2  A: 

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

p.Margin = new Thickness(0);
Darren Oster
+13  A: 

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>
moogs
I like this solution the best. Not only because it is done with style, but because it is DONE with style.
David Basarab
Nice solution, but won't you have to use code if you are formatting a selection, rather than all text in the box?
David Veeneman
you can do styles with code, too.
moogs
Thanks so much for this! I hate how RichTextBox inserts an extra "line" on Enter, I find it so unfamiliar, and was dreading the solution would be incredibly difficult. Boy am I relieved!
chaiguy
A: 

I'd like to do that by code for all the paragraphs of my richtextbox, is it possible ?

Ballchris13
See moogs' answer.
chaiguy
A: 
<RichTextBox  Height="250" Width="500" VerticalScrollBarVisibility="Auto" TextWrapping="Wrap" IsReadOnly="True" >
                    <Paragraph>

                       XYZ

                        <LineBreak></LineBreak>
                    </Paragraph>
</RichTextBox>
Haasan Sachdev
A: 

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

Danny