So I want to insert three lines (not paragraphs) into my Q2 2014 Richtextbox. When I do editor.InsertLine("") I get a paragraph. I can set the ParagraphDefaultSpacingAfter to 0 and get my desired effect, but I would rather just insert a line.
How would I do that?
5 Answers, 1 is accepted
You can insert a line break with the Alt+Enter keyboard shortcut. Programmatically this is achieved with the InsertLineBreak() method of RadRichTextBox or RadDocumentEditor.
this
.radRichTextBox.InsertLineBreak();
Note that lines separated in this manner are still interpreted as part of the same Paragraph document element. What you should be aware of is that extremely large document elements can cause performance diminishment when scrolling and typing in the control, so I would not recommend replacing all paragraph ends with a line break. Instead, you can simply change the default spacing after of the Normal style:
this
.radRichTextBox.Document.StyleRepository.GetValueOrNull(RadDocumentDefaultStyles.NormalStyleName).ParagraphProperties.SpacingAfter = 0;
I hope this is useful.
Regards,
Petya
Telerik
I've been through a number of posts here on the forum concerning line and paragraph spacing and tried everything suggested by Telerik admins and still cannot find a way to control paragraph spacing as it always defaults to double line.
My RadRichTextBox is setup for data binding using the Telerik data provider as such:
xmlns:t="http://schemas.telerik.com/2008/xaml/presentation"
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="0"/>
</Grid.RowDefinitions>
<t:RadRichTextBox x:Name="RtbTextEditor" Grid.Row="0"/>
<t:TxtDataProvider x:Name="TxtDataProvider" Grid.Row="1" Text="{Binding RtbContent, Mode=TwoWay}"
UpdateSourceTrigger="LostFocus" RichTextBox="{Binding ElementName=RtbTextEditor}" />
</Grid>
I first tried to set Paragraph Default Spacing in XAML:
<t:RadRichTextBox x:Name="RtbTextEditor" Grid.Row="0">
<t:RadRichTextBox.Document>
<t:RadDocument ParagraphDefaultSpacingAfter="0" ParagraphDefaultSpacingBefore="0"/>
</t:RadRichTextBox.Document>
</t:RadRichTextBox>
That did not work so I tried to set it in code after the SetupDocument event is fired as such:
private void TxtDataProvider_OnSetupDocument(object sender, SetupDocumentEventArgs e)
{
RtbTextEditor.Document.ParagraphDefaultSpacingAfter = 0;
RtbTextEditor.Document.ParagraphDefaultSpacingBefore = 0;
}
Finally I tried using the StyleRepository property of the Document:
private void TxtDataProvider_OnSetupDocument(object sender, SetupDocumentEventArgs e)
{ RtbTextEditor.Document.StyleRepository.GetValueOrNull(RadDocumentDefaultStyles.NormalStyleName).ParagraphProperties.SpacingAfter = 0;
}
I verified that this code executes after SetupDocument event fires and the properties are changed but none of that worked and when I press Enter it always defaults to double line spacing and the only way to get the single line spacing is to press Shift-Enter.
Any ideas on how to get the paragraph default spacing to change to single line?
Finally, I moved the ParagraphDefaultSpacingAfter setter to DocumentChanged event and that worked...
private void RtbTextEditor_OnDocumentChanged(object sender, EventArgs e)
{
RtbTextEditor.Document.ParagraphDefaultSpacingAfter = 0;
}
There are several approaches which you can use to achieve the desired behavior. The ones you are using are correct but need some slight modifications so they can work as expected.
Setting in XAML
You can set the default spacing before and after a paragraph. In the snippet from your first post, the property is applied only for the first instance of RadDocument, which is changed when opening another document. To ensure the settings you are applying are inherited when opening a new document, you should set the DocumentInheritsDefaultStyleSettings property to true.
Through SetupDocument event
When this event is raised, the imported document is still not set to the RadRichTextBox instance. Thus, you should set the desired properties to the Document property exposed by the event args:
private
void
TxtDataProvider_SetupDocument(
object
sender, Telerik.Windows.Documents.FormatProviders.SetupDocumentEventArgs e)
{
e.Document.ParagraphDefaultSpacingAfter = 0;
e.Document.ParagraphDefaultSpacingBefore = 0;
}
Hope this is helpful.
Regards,
Tanya
Progress Telerik