New to Telerik UI for WPF? Download free 30-day trial

Formatting API

If you want to format the content of RadRichTextBox at run time, you have to use the API exposed by RadRichTextBox. This is essential, as the main purpose of RadRichTextBox is to allow the users to format their input via UI. The UI should call the respective API methods of the control.

To learn more about the methods exposed by the API take a look at this topic.

Changing the text formatting

The RadRichTextBox exposes methods that change the style of the selected 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.

RadRichTextBox provides a fully functional formatting UI out of the box. To learn more about it read the RadRichTextBoxRibbonUI topic.

Here is an example of a toggle button that upon checking should make the selection or the current word bold. In the handler of the Click event of the RadToggleButton, the ToggleBold() method of RadRichTextBox is 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) 
End Sub 

WPF RadRichTextBox Bolded Text

Using the active editor

RadRichTextBox supports headers and footers. They are represented through separate instances of RadRichTextBox. When a document has headers and footers you can use the ActiveDocumentEditor property of RadRichTextBox to get the editor instance where the caret is currently situated.

You can find more about the Header and Footer functionality in this article.

The following example inserts the word "text" at the CaretPosition.


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:

  • through DocumentFragment's constructor;

  • through selection.

Both approaches can be used to insert content at the caret position with the InsertFragment method:


Using the constructor of DocumentFragment

If you create a 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 fragmentFromDocument = new DocumentFragment(radDocument); 
Dim fragment As New DocumentFragment(radDocument) 

This is also the suggested approach 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 fragmentFromSelection = radDocument.Selection.CopySelectedDocumentElements(); 
Dim fragment As DocumentFragment = document.Selection.CopySelectedDocumentElements() 

For merging RadDocument instances, you can use the RadDocumentMerger class.

See Also

In this article