Clipboard Support

RadRichTextBox takes advantage of the clipboard support in Silverlight. However, as Silverlight's clipboard supports only plain text, copied content will be stripped of all formatting. For the needs of the control, it has a built-in rich text clipboard that allows copy/paste of text along with its formatting in one rich text box or between rich text boxes in the same application.

Furthermore, the API is extensible, so that you can implement a custom external clipboard, provided that you have the ability to obtain rich text from the clipboard.

Methods and Commands

RadRichTextBox's API exposes a method and a command for each of the three actions that can be performed against the clipboard: Cut, Copy and Paste.

Example 1: Using the exposed methods to interact with the clipboard

this.radRichTextBox.Copy(); 
this.radRichTextBox.Cut(); 
this.radRichTextBox.Paste(); 
Me.radRichTextBox.Copy() 
Me.radRichTextBox.Cut() 
Me.radRichTextBox.Paste() 

When building UI for the RichTextBox, the respective commands can be used:

Example 2: Wiring UI to the commands related to the clipboard

<telerik:RadToolBar DataContext="{Binding ElementName=editor, Path=Commands}"> 
  <telerik:RadRibbonButton telerik:ScreenTip.Title="Cut" 
               telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=CutCommand}"  
               Text="Cut" /> 
  <telerik:RadRibbonButton telerik:ScreenTip.Title="Copy" 
               telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=CopyCommand}"  
               Text="Copy" /> 
  <telerik:RadRibbonButton telerik:ScreenTip.Title="Paste" 
               telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=PasteCommand}" 
               Text="Paste" /> 
</telerik:RadToolBar> 

Note that the attached telerik:RadRichTextBoxRibbonUI.RichTextCommand property works only with Ribbon buttons. With regular RadButtons and other buttons, you should use their Command property instead.

Key Bindings

In order to copy, paste or cut, the standard keyboard shortcuts can also be used - Ctrl + C, Ctrl + V, Ctrl + X.

To learn more about the default key-bindings of the editor and ways to override them, you can refer to the Keyboard Support article.

Settings

In order to work around the limitation of the plain text only clipboard in Silverlight, we have exposed API for setting and getting the clipboard content of RadRichTextBox.

In that regard, we have introduced an interface – IExternalClipboard, which contains the following members:

Example 3: IExternalClipboard

public interface IExternalClipboard 
{ 
    bool ContainsFragment(); 
    DocumentFragment GetFragment(); 
    void SetFragment(DocumentFragment fragment); 
} 

Basically, you should implement the interface and set the static property of ClipboardEx named ExternalClipboard:

Example 4: Setting external clipboard

ClipboardEx.ExternalClipboard = new RichTextExternalClipboard(); 

This will ensure that your methods will be used when the following operations are invoked:

  • Cut and Copy – in these cases the SetFragment(DocumentFragment fragment) method will be called. The fragment parameter includes the fragment that is currently copied, so that you can set it to the clipboard.

  • Paste – here the methods used are ContainsDocument() and GetFragment(). Basically, when the content of the clipboard is being set externally, a DocumentFragment must be created out of it. When paste is initiated, this DocumentFragment will be requested using the GetFragment() method if the ContainsFragment() method returns true.

See Also

In this article