If you want to format the content of the RadRichTextBox at run time, you have to use the API exposed by the RadRichTextBox. This is essential, as the main purpose of the RadRichTextBox is to allow the users to format their input via UI. The UI should call the respective API methods of the RadRichTextBox.
To learn more about the methods exposed by the API take a look at
Changing the text formatting
The RadRichTextBox exposes methods that change the style of the text or the paragraph. When a method is called, the respective style is applied to the selected text. If there is no selection available, the style is applied to the word, in which the caret is located.
The RadRichTextBox provides a fully functional formatting UI out of the box. To learn more about it read the RadRichTextBoxRibbonUI topic.
To learn how to make the UI response to the styles at the current caret position, read here.
Here is an example of a toggle button that upon checking should make the selection or the current word bold. In the handler for the Click event of the RadToggleButton, the ToggleBold() method of the RadRichTextBox gets called.
<telerik:RadToggleButton x:Name="BoldButton" Content="B" Padding="5" HorizontalAlignment="Left" Click="BoldButton_Click" />
<telerik:RadRichTextBox x:Name="radRichTextBox" LayoutMode="Paged" Height="200" />
private void BoldButton_Click(object sender, RoutedEventArgs e)
Private Sub BoldButton_Click(sender As Object, e As RoutedEventArgs)
Using the active editor
RadRichTextBox supports headers and footers. They are represented through separate instances of RadRichTextBox. When your document has headers and footers you can use the ActiveDocumentEditor property to get the instance where your caret is currently situated.
You can find more about the Header and Footer functionality in
The following example inserts the word "text" at the CaretPosition.
(this.radRichTextBox.ActiveDocumentEditor as RadRichTextBox).Document.InsertInline(new Span("text"));
TryCast(Me.radRichTextBox.ActiveDocumentEditor, RadRichTextBox).Document.InsertInline(New Span("text"))
Creating a DocumentFragment
One of the common uses of the API is creating and inserting a DocumentFragment. Currently you can create a fragment in two ways:
Both approaches can be used to insert text at the caret position with the InsertFragment method:
Using the constructor of DocumentFragment
If you create the fragment in this way, it will end with a new paragraph. This is convenient when you want to separate the inserted fragment and end it with a new line. Furthermore, in this way if the last paragraph is in a list, it will appear properly in the new document.
DocumentFragment fragment = new DocumentFragment(radDocument);
Dim fragment As New DocumentFragment(radDocument)
This is also the approach used when merging several documents into one.
Using the selection
If you choose to use the document selection when creating a DocumentFragment, there will be no additional paragraph after the fragment.
DocumentFragment fragment = document.Selection.CopySelectedDocumentElements();
Dim fragment As DocumentFragment = document.Selection.CopySelectedDocumentElements()