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
2008-11-28 05:07:16
This is not enough if they can change font size.
Donnelle
2008-11-28 06:35:50
+2
A:
Close, so you got the points. Actually it turned out to be setting the margin,
p.Margin = new Thickness(0);
Darren Oster
2008-11-28 05:31:08
+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
2009-01-15 07:06:48
I like this solution the best. Not only because it is done with style, but because it is DONE with style.
David Basarab
2009-06-12 12:49:30
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
2010-03-22 15:26:07
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
2010-05-30 02:08:06
A:
I'd like to do that by code for all the paragraphs of my richtextbox, is it possible ?
Ballchris13
2010-04-21 12:30:49
A:
<RichTextBox Height="250" Width="500" VerticalScrollBarVisibility="Auto" TextWrapping="Wrap" IsReadOnly="True" >
<Paragraph>
XYZ
<LineBreak></LineBreak>
</Paragraph>
</RichTextBox>
Haasan Sachdev
2010-05-06 13:25:22
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
2010-10-28 11:02:11