Context Menu

The RadRichTextBox UI has a built-in context menu feature which can be used to easily customize different elements in a document. The menu is displayed when you mouse right click on the RadRichTextBox control. It contains some context specific commands arranged in groups. There are groups for spellchecking, clipboard, table editing and text editing.

The context menu is available only when the Telerik.Windows.Controls.RichTextBoxUI dll is referenced in the project.

Figure 1: Menu in the context of a table

Silverlight RadRichTextBox Menu in the context of a table

The context menu is enabled by default. You can control this with the IsContextMenuEnabled property. If you set the property to False, the Selection Mini Tool Bar will be displayed when you click the mouse right button.

Example 1: Disabling the context menu

<telerik:RadRichTextBox IsContextMenuEnabled="False"/> 

The menu is accessible through the ContextMenu property of RadRichTextBox control.

The context menu instance is cached and shared between all the instances of RadRichTextBox in an application.

The RichTextBox default context menu can be fully replaced by an object that implements the IContextMenu interface which is marked with CustomContextMenuAttribute. Additionally, the menu can be customized by adding, removing and modifying menu groups and items. You can do that by using the Showing event of the menu or by creating a custom content builder and override its construction methods.

Using the Showing Event

You can customize the default context menu by subscribing for its Showing event. The following example shows how to add menu items.

Example 2: Subscribing to the Showing event

Telerik.Windows.Controls.RichTextBoxUI.ContextMenu contextMenu = (Telerik.Windows.Controls.RichTextBoxUI.ContextMenu)this.radRichTextBox.ContextMenu; 
contextMenu.Showing += RichTextBox_ContextMenuShowing; 

Example 3: Adding and removing menu items

private void RichTextBox_ContextMenuShowing(object sender, Telerik.Windows.Controls.RichTextBoxUI.Menus.ContextMenuEventArgs e) 
{ 
    if (!this.radRichTextBox.Document.Selection.IsEmpty) 
    { 
        RadMenuItem menuItem = new RadMenuItem() 
        { 
            Header = "Change selection foreground" 
        }; 
        menuItem.Click += this.OnChangeSelectionForeground; 
 
        ContextMenuGroup contextMenuGroup = new ContextMenuGroup(); 
        contextMenuGroup.Add(menuItem); 
        e.ContextMenuGroupCollection.Add(contextMenuGroup); 
    } 
} 
 
private void OnChangeSelectionForeground(object sender, RadRoutedEventArgs e) 
{ 
    this.radRichTextBox.ChangeTextForeColor(Colors.Red); 
} 

The Showing event is not presented in the IContextMenu interface so you will need to cast the ContextMenu property to Telerik.Windows.Controls.RichTextBoxUI.ContextMenu.

Figure 2: Executing the action of the new added item

Silverlight RadRichTextBox Executing the action of the new added item

You can use this approach also to remove or modify menu groups and items. They can be accessed via the ContextMenuGroupCollection property of the event arguments.

Creating Custom Content Builder

You can customize the default context menu by creating a custom content builder and assign it to the ContentBuilder property of the ContextMenu.

Example 4: Creating custom content builder

public class CustomContextMenuBuilder : ContextMenuContentBuilder 
{ 
    public override ContextMenuGroupCollection Construct() 
    { 
        var groupsCollection = base.Construct(); 
        if (!this.RadRichTextBox.Document.Selection.IsEmpty) 
        { 
            RadMenuItem menuItem = new RadMenuItem() 
            { 
                Header = "Change selection foreground" 
            }; 
            menuItem.Click += this.OnChangeSelectionForeground; 
 
            ContextMenuGroup contextMenuGroup = new ContextMenuGroup(); 
            contextMenuGroup.Add(menuItem); 
            groupsCollection.Add(contextMenuGroup);                 
        } 
        return groupsCollection; 
    } 
 
    private void OnChangeSelectionForeground(object sender, RadRoutedEventArgs e) 
    { 
        this.RadRichTextBox.ChangeTextForeColor(Colors.Red); 
    } 
} 

Example 5: Setting the content builder

Telerik.Windows.Controls.RichTextBoxUI.ContextMenu contextMenu = (Telerik.Windows.Controls.RichTextBoxUI.ContextMenu)this.radRichTextBox.ContextMenu; 
contextMenu.ContentBuilder = new CustomContextMenuBuilder(); 

Figure 2 shows how this modification affects the context menu.

The ContextMenuContentBuilder class exposes several method overrides which can be used to customize different groups from the context menu. The following list contains the overrides with short descriptions.

  • Construct: This method creates all groups allowed in the specific context determined by the RadRichTextBox control.
  • CreateClipboardCommands: This method creates the groups containing menu items with commands related to the clipboard (copy, cut, paste, etc.).
  • CreateCodeBlockCommands: This method creates the groups containing menu items with commands related to the code blocks.
  • CreateFieldCommands: This method creates the groups containing menu items with commands related to the fields.
  • CreateFloatingBlockCommands: This method creates the groups containing menu items with commands related to the floating blocks.
  • CreateHeaderFooterCommands: This method creates the groups containing menu items with commands related to the headers/footers.
  • CreateHyperlinkCommands: This method creates the groups containing menu items with commands related to the hyperlinks.
  • CreateImageCommands: This method creates the groups containing menu items with commands related to the images.
  • CreateListCommands: This method creates the groups containing menu items with commands related to the lists.
  • CreateSpellCheckingSuggestions: This method creates the groups containing menu items with commands releated to the spellchecking suggestions.
  • CreateTableCommands: This method creates the groups containing menu items with commands related to the tables.
  • CreateTextEditCommands: This method creates the groups containing menu items with commands related to the text editing.
  • CreateTrackChangesCommands: This method creates the groups containing menu items with commands related to the track changes.

If you prefer, instead of deriving from the ContextMenuContentBuilder class, the custom builder can implement the IContextMenuContentBuilder interface and construct the groups from scratch.

Using Different Context Menus

By default, the context menu is cached and used by all instances of RadRichTextBox in the application. If you would like to use separate context menus for the different instances of RadRichTextBox, you can use the code from Example 6 to reset that behavior.

Example 6: Use separate context menu for an instance of RadRichTextBox

this.radRichTextBox.ContextMenu = new Telerik.Windows.Controls.RichTextBoxUI.ContextMenu(); 
Telerik.Windows.Controls.RichTextBoxUI.ContextMenu contextMenu = (Telerik.Windows.Controls.RichTextBoxUI.ContextMenu)this.radRichTextBox.ContextMenu; 
contextMenu.ContentBuilder = new CustomContextMenuBuilder(); 

See Also

In this article