Edit this page

Clipboard Support

RadRichTextBox takes advantage of the clipboard support in WPF. As the clipboard keeps content in several formats, it is possible to choose which input to use and further customize the way the text will look when pasted.

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. They can be evoked as follows:

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:

<telerik:RadToolBar DataContext="{Binding ElementName=editor, Path=Commands}">
  <telerik:RadRibbonButton telerik:ScreenTip.Title="Cut" telerik:ScreenTip.Description="Cut the selection and put it on the Clipboard." CollapseToSmall="WhenGroupIsMedium" telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=CutCommand}" Size="Medium" SmallImage="pack://application:,,,/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/cut.png" Text="Cut" />
  <telerik:RadRibbonButton telerik:ScreenTip.Title="Copy" telerik:ScreenTip.Description="Copy the selection and put it on the Clipboard." CollapseToSmall="WhenGroupIsMedium" telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=CopyCommand}" Size="Medium" SmallImage="pack://application:,,,/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/copy.png" Text="Copy" />
  <telerik:RadRibbonButton telerik:ScreenTip.Title="Paste" telerik:ScreenTip.Description="Paste the contents of the Clipboard." CollapseToSmall="WhenGroupIsMedium" telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=PasteCommand}" Size="Medium" SmallImage="pack://application:,,,/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/paste.png" 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.

Settings

By default the PasteCommand of the editor uses its RtfFormatProvider and HtmlFormatProvider in order to create a RadDocument out of the document in the clipboard and insert it in the current document. That is why in order to be able to paste rich content, at least one of these two assemblies must be referenced:

  • Telerik.Windows.Documents.FormatProviders.Rtf.dll

  • Telerik.Windows.Documents.FormatProviders.Html.dll

More information on the use of the format providers for importing text can be found here: Format Providers

When the two format providers are referenced, the PasteCommand tries to create a document out of the RTF content of the clipboard. If it fails for some reason (e.g. the RTF is invalid or not supported by RtfFormatProvider), the paste falls back to HTML. In case HtmlFormatProvider cannot handle the clipboard content as well, the plain text from the clipboard will be inserted in the document.

You can customize this behavior by removing or reordering the clipboard handlers for the different formats.

Each Clipboard Handler contains the following properties:

  • DocumentFormatProvider: The format provider used to import the data in the clipboard, e.g. HtmlFormatProvider or RtfFormatProvider.

  • ClipboardDataFormat: The format of the data (DataFormats.Html or DataFormats.Rtf).

  • ClipboardStringFilter: A filter used to preprocess the data if needed. For example, the HTML that Word puts in the clipboard contains some headings which should be stripped in order to acquire the clean HTML content before passing it to a format provider. Such a filter is ClipboardEx.StripHtmlClipboardFormatHeaders, but you can also pass any Func.

For instance, here is how you can clear the default clipboard handlers and add only a handler which uses TxtFormatProvider. In this way only plain text will be pasted when you copy from a source such as another rich text editor or a browser.

ClipboardEx.ClipboardHandlers.Clear();

ClipboardHandler clipboardHandler = new ClipboardHandler();
clipboardHandler.ClipboardDataFormat = DataFormats.Text;
clipboardHandler.DocumentFormatProvider = new TxtFormatProvider();

ClipboardEx.ClipboardHandlers.Add(clipboardHandler);
ClipboardEx.ClipboardHandlers.Clear()

Dim clipboardHandler As New ClipboardHandler()
clipboardHandler.ClipboardDataFormat = DataFormats.Text
clipboardHandler.DocumentFormatProvider = New TxtFormatProvider()

ClipboardEx.ClipboardHandlers.Add(clipboardHandler)

You can also reorder the clipboard handlers to use first HTML paste and fallback to RTF instead of the other way around, or attach to different events of the format providers.

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 this article: Keyboard Support.

See Also