This is a migrated thread and some comments may be shown as answers.

Custom Clipboard Format for Copy\Paste

3 Answers 563 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 28 Feb 2017, 02:19 PM

We have an application in which we would like to support a custom clipboard format for copying\pasting in a RadRichTextBox. I have read your article on clipboard support (http://docs.telerik.com/devtools/wpf/controls/radrichtextbox/features/clipboard-support), but that seems to mainly deal with changing which provided clipboard formats are used.

It appears that if I register a custom format provider with ClipboardEx, that would be able to handle pasting the custom format. For example:

ClipboardEx.ClipboardHandlers.Insert(0, new ClipboardHandler()
{
    ClipboardDataFormat = "MyCustomDataFormat",
    DocumentFormatProvider = new MyCustomFormatProvider()
});

 

However, I don't see a way to handle copying a custom format without intercepting the CopyExecutingCommand and adding the custom format the the clipboard. For example:

void RadRichTextBox_CommandExecuting(object sender, CommandExecutingEventArgs e)
{
    if (e.Command is CopyCommand)
    {
        Clipboard.SetData("MyCustomDataFormat", GetSelectionAsCustomDataFormatString());
    }
}

However, I would want to make sure that the default clipboard formats are still added to the clipboard in addition to our custom format.

Can you provide any guidance on the best way to handle copying and pasting a custom clipboard format?

Thank you.

3 Answers, 1 is accepted

Sort by
0
Boby
Telerik team
answered on 02 Mar 2017, 09:55 AM
Hello Chris,

You can use CommandExecuted event,  "clone" the original data object and enhance it with the custom format, e.g.:
this.radRichTextBox.CommandExecuted += (sender, e) =>
{
    if (e.Command is CopyCommand)
    {
        var originalDataObject = Clipboard.GetDataObject();
 
        DataObject dataObject = new DataObject();
        foreach (string format in originalDataObject.GetFormats())
        {
            dataObject.SetData(format, originalDataObject.GetData(format));
        }
 
        dataObject.SetData("Html", new HtmlFormatProvider().Export(this.radRichTextBox.Document));
 
        Clipboard.SetDataObject(dataObject);
    }
};

Your assumption that registered handlers should be used for copying as well is actually very reasonable. I logged it as a feature request in our public feedback portal here: RichTextBox: Enhance clipboard handlers API and use them for Copy operation. We have added some points to your account for the suggestion. 

Regards,
Boby
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Chris
Top achievements
Rank 1
answered on 03 Mar 2017, 12:53 PM

Thank you for your help. That works for putting data on the clipboard. However, my custom clipboard handler is not being used. It appears that if there is a format of RadDocumentGUID on the clipboard, all of the clipboard handlers are ignored. I was able to get around this by modifying the code you provided to use the following foreach.

foreach (string format in originalDataObject.GetFormats().Where(f => f != "RadDocumentGUID"))
{
    dataObject.SetData(format, originalDataObject.GetData(format));
}

 

Is there a better way to handle this?

0
Boby
Telerik team
answered on 06 Mar 2017, 08:16 AM
Hello Chris,

Current Paste implementation always uses the native RadDocument format for copy-pasting between RadRichTextBoxes, and this is not subject to configuring with ClipboardHandlers - so your approach will do the work - if RadDocumentGUID is not present in the clipboard data, the first registered clipboard handler will be used. 

Regards,
Boby
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Tags
RichTextBox
Asked by
Chris
Top achievements
Rank 1
Answers by
Boby
Telerik team
Chris
Top achievements
Rank 1
Share this question
or