Edit this page

Commands

The RadRichTextBox control exposes a full set of commands that that provide an alternative to its API methods, exposed through its Commands property. For each of the Formatting API methods there is a respective command. In order to use the command with an UI Element that supports commanding, you have to bind the Command property of the element to the respective command of the RadRichTextBox.

To see a list of all the commands exposed by RadRichTextBox, visit this topic.

Here is an example with a RadToggleButtonControl.

XAML

<telerik:RadToggleButton x:Name="BoldButton"
                            Content="Bold"
                            DataContext="{Binding Commands, ElementName=radRichTextBox}"
                            Command="{Binding ToggleBoldCommand}" />
<telerik:RadRichTextBox x:Name="radRichTextBox"
                                    Height="500"
                                    Width="500">
    <telerik:RadDocument LayoutMode="Paged" />
</telerik:RadRichTextBox>

Now every time the button is clicked it will toggle the boldness of the current selection. The thing it won't do is to response to the current state of the selection. For example if the context of the caret is a bold text, the button won't get automatically toggled. In order to implement this behavior you have to handle the ToggleStateChanged event of the ToggleBoldCommand. Here is an example.

C#

public CommandsSample()
{
    InitializeComponent();
    this.radRichTextBox.Commands.ToggleBoldCommand.ToggleStateChanged += this.ToggleBoldCommand_ToggleStateChanged;
}
public void ToggleBoldCommand_ToggleStateChanged( object sender, StylePropertyChangedEventArgs<bool> e )
{
    this.BoldButton.IsChecked = e.NewValue;
}

VB.NET

Public Sub New()
    InitializeComponent()
    Me.radRichTextBox.Commands.ToggleBoldCommand.ToggleStateChanged += Me.ToggleBoldCommand_ToggleStateChanged
End Sub
Public Sub ToggleBoldCommand_ToggleStateChanged(sender As Object, e As StylePropertyChangedEventArgs(Of Boolean))
    Me.BoldButton.IsChecked = e.NewValue
End Sub

Now the button will respond to the current state of the selection.

Rad Rich Text Box Features Commands 01

You can also use the functionality provided by the RadRichTextBoxRibbonUI class in order to get the event handling out of the box.

The RadRichTextBoxRibbonUI provides you with a fully predefined UI for the RadRichtextBox. To learn how to use it read this topic.

If you are building your UI manually ,for example using a RadRibbonView control, you have to only bind the command to the RadRichTextBoxRibbonUI.RichTextCommand attached property of the desired RadRibbonView control. Here is an example with a RadRibbonToggleButton.

XAML

<telerik:RadRibbonToggleButton x:Name="ItalicButton"
                                Content="Italic"
                                DataContext="{Binding Commands, ElementName=radRichTextBox}"
                                telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding ToggleItalicCommand}" />

The RadRibbonToggleButton will get automatically toggled whenever the context of the caret is an italic text.

More about the specific buttons introduced with RadRibbonView you can find here

Rad Rich Text Box Features Commands 02

Modifying default application commands bindings

By default, every RadRichTextBox instance defines built-in mappings between ApplicationCommands and its Commands:

Application Command RadRichTextBox Command
ApplicationCommands.Cut CutCommand
ApplicationCommands.Copy CopyCommand
ApplicationCommands.Paste PasteCommand
ApplicationCommands.Delete DeleteCommand
ApplicationCommands.Find ShowFindReplaceDialogCommand
ApplicationCommands.New NewDocumentCommand
ApplicationCommands.Open OpenDocumentCommand
ApplicationCommands.Save SaveCommand
ApplicationCommands.SelectAll SelectAllCommand
ApplicationCommands.Undo UndoCommand
ApplicationCommands.Redo RedoCommand
ApplicationCommands.Print PrintCommand

You can remove some of the bindings by using RadRichTextBox.RegisteredApplicationCommands collection:

C#

this.radRichTextBox.RegisteredApplicationCommands.Remove(ApplicationCommands.Save);

and re-add them (as long as they are among the default mappings list) at later time:

C#

this.radRichTextBox.RegisteredApplicationCommands.Add(ApplicationCommands.Save);

If you want to handle application commands on RadRichTextBox owner's level, you should suppress the default handing mechanism for the specified shortcut using RadRichTextBox.PreviewEditorKeyDown event, for example:

C#

this.radRichTextBox.PreviewEditorKeyDown += (sender, e) =>
    {
        if (Keyboard.IsKeyDown(Key.LeftCtrl) && e.Key == Key.S)
        {
            e.SuppressDefaultAction = true;
        }
    };

VB.NET

Me.radRichTextBox.PreviewEditorKeyDown += Function(sender, e) Do
    If Keyboard.IsKeyDown(Key.LeftCtrl) AndAlso e.Key = Key.S Then
        e.SuppressDefaultAction = True
    End If
End Function