RadControls for Silverlight

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.

Note

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 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.

Tip

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

Tip

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.

CopyXAML
<StackPanel>
    <telerik:RadToggleButton x:Name="BoldButton" Content="B" Padding="5" HorizontalAlignment="Left" Click="BoldButton_Click" />
    <telerik:RadRichTextBox x:Name="radRichTextBox" LayoutMode="Paged" Height="200" />
</StackPanel>
CopyC#
private void BoldButton_Click(object sender, RoutedEventArgs e)
{
    this.radRichTextBox.ToggleBold();
}
CopyVB.NET
Private Sub BoldButton_Click(sender As Object, e As RoutedEventArgs)
    Me.radRichTextBox.ToggleBold()
End Sub

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.

Tip

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

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

CopyC#
(this.radRichTextBox.ActiveDocumentEditor as RadRichTextBox).Document.InsertInline(new Span("text"));
CopyVB.NET
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:

  • through DocumentFragment's constructor;

  • through the selection.

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

CopyC#
this.radRichTextBox.Document.InsertFragment(fragment);

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.

CopyC#
DocumentFragment fragment = new DocumentFragment(radDocument);
CopyVB.NET
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.

CopyC#
DocumentFragment fragment = document.Selection.CopySelectedDocumentElements();
CopyVB.NET
Dim fragment As DocumentFragment = document.Selection.CopySelectedDocumentElements()

See Also