1

I have a TextBox in my WPF application which I've added a Paste event to using:

DataObject.AddPastingHandler(elm, new DataObjectPastingEventHandler(OnPaste));

Now I want to trigger the OnPaste event from C# code. How can I do this? I tried calling the Paste() function on the control. The text is pasted in the control, but the OnPaste event is not triggered..:

private static void Foo(TextBox textBox, string pastedText)
{
    Clipboard.SetData(DataFormats.Text, pastedText);
    textBox.Paste();
}
stiank81
  • 25,418
  • 43
  • 131
  • 202

2 Answers2

5

Invoke the ApplicationCommand Paste:

ApplicationCommands.Paste.Execute(this, pastedText)

Please note that this will not work in partial trust!

Arcturus
  • 26,677
  • 10
  • 92
  • 107
  • Thx. The code I suggested in the question actually works too.. My bad. Problem was that the pasting handler must be set manually, and I set it in some part of the code that wasn't triggered in my unit test.. – stiank81 Aug 06 '10 at 08:48
1

You could do it via reflection. You need to find the private delegate field, then invoke it.

See How to: Hook Up a Delegate Using Reflection.

Winston Smith
  • 21,585
  • 10
  • 60
  • 75