4

Is there any way to capture the paste event in an AvalonEdit text editor so that the data can be modified?

We are having users paste data from Excel, and for some reason that data is showing up with an extra newline. This means that if they paste while block-selecting it throws off the last line.

Ideally I'd like to catch the paste event, modify the contents to either remove the newline or format it differently, and then deliver the modified event... I just can't find where the paste handler might be (if it's exposed at all) in the TextEditor or TextView.

zaknotzach
  • 977
  • 1
  • 9
  • 18

1 Answers1

3

The code in AvalonEdit handling the Paste command is the internal EditingCommandHandler.OnPaste().

It allows using the DataObject.Pasting attached event to customize the paste behavior.

Daniel
  • 15,944
  • 2
  • 54
  • 60
  • 1
    That definitely put me on the right track. Just to give more details for any future readers: I added this in the constructor `DataObject.AddPastingHandler(this, PasteEvent);` and then in the paste event `private void PasteEvent(object sender, DataObjectPastingEventArgs args)` you can get the data from the args. You can't modify the data object so you have to make a new one, such as in this: https://stackoverflow.com/a/11306145/3723624 – zaknotzach Dec 06 '17 at 19:44