0

I'm developing a WPF application, and I'd like to catch the paste command in a RichTextBox input and handle the pasted files. For this I'm using the following callback:

<RichTextBox DataObject.Pasting="BodyRichTextBox_Pasting">
private void BodyRichTextBox_Pasting(object sender, DataObjectPastingEventArgs e) {
    // handler 1
}

I can handle the text and image pasting, but when I'd like to paste files, then this callback doesn't get fired.

I also can catch the paste event with:

<RichTextBox.CommandBindings>
    <CommandBinding Command="ApplicationCommands.Paste" Executed="CommandBinding_Executed" />
</RichTextBox.CommandBindings>
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e) {
  // handler 2
}

But this callback overrides the previous one, and I need important fields from the DataObjectPastingEventArgs variable. So I wanted to use the first handler to handle text and image pasting and the second for file pasting, but doesn't works both together (If I set e.Handled to false doesn't help).

Any other idea how to do this?

1 Answer 1

2

You could handle the ApplicationCommands.Paste command something like this to get the pasted in text, file(s) or image:

private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
    if (e.Command == ApplicationCommands.Paste)
    {
        var files = Clipboard.GetFileDropList();
        var text = Clipboard.GetData(DataFormats.Text);
        var image = Clipboard.GetImage();
        e.Handled = true;
    }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.