I have some code that is designed to alter what is in the clipboard on cut/copy, and it works perfectly for Copy, but I can't get Cut to work.
In the xaml, I've defined a RichTextBox named rtbEditor, in the Loaded event, I set up the CopyingHandler:
DataObject.AddPastingHandler(rtbEditor, new DataObjectPastingEventHandler(OnPaste));
DataObject.AddCopyingHandler(rtbEditor, new DataObjectCopyingEventHandler(OnCopy));
OnCopy is (simplified):
private void OnCopy(object sender, DataObjectCopyingEventArgs e)
{
// Expand the selection include whole paragraphs only:
TextPointer newStart = rtbEditor.Selection.Start.Parapgraph.ContentStart;
TextPointer newEnd = rtbEditor.Selection.End.Paragraph.ContentEnd;
rtbEditor.Selection.Select(newStart, newEnd);
// copy the selected text
TextRange range = new TextRange(rtbEditor.Selection.Start, rtbEditor.Selection.End);
Clipboard.SetText(range.Text);
e.CancelCommand();
}
This works wonders for Copy, but I'm running into problems making Cut work.
I tried simply expanding the selection with rtbEditor.Selection.Select() and that was it, but the DataObject containing the copied data has been filled already by the time the CopyingHandler has been called, so changing the selection doesn't change what will be placed in the clipboard. (I'm still doing it for the visual feedback to the user that their selection has been expanded)
If I remove e.CancelCommand(), then the cut will correctly delete the text, but only the text that was originally selected and not the expanded selection, and the clipboard will contain only the text that was originally selected, and not the expanded selection either. I'm assuming because since the command isn't cancelled, my Clipboard.SetText() is immediately overwritten by the contents of the DataObject when the cut command finishes.
I also can't find anything in sender or DataObjectCopyingEventArgs that distinguishes whether this event is a Cut event or a Copy event so I can have my code delete the text if it's a Cut.
Is there a way to distinguish here between Cut and Copy that I'm not seeing? Or is there some event I can hook in to that is earlier in the process? MSDN says the CopyingHandler happens "when a copy operation has finished converting the selected content...". But I can't find any event that occurs before the copy operation starts. Or do I need to approach this in a completely different manner?
I found a similar question to this in the comments of How to override copy and paste in richtextbox but it had no answers there