tags:

views:

81

answers:

2

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();
}
+1  A: 

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
+2  A: 

Invoke the ApplicationCommand Paste:

ApplicationCommands.Paste.Execute(this, pastedText)

Please note that this will not work in partial trust!

Arcturus
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