WPF simulate mouse click in code behind

Let’s say there is an event captured on a control and you want to simulate a click of a button to fire whatever that button is bound to do, there are a few ways of doing this, one that I’ve used before is using an AutomationPeer:

AutomationPeer button1AP = UIElementAutomationPeer.CreatePeerForElement(button1);
(button1AP.GetPattern(PatternInterface.Invoke) as IInvokeProvider).Invoke();

Another option is to use:

button1.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
About these ads

3 responses to “WPF simulate mouse click in code behind

  1. I fear I just went for the old-fashioned…

    Button on-click method calls method X
    Other event calls method X

    I shall look up your crazy s**t now…

  2. Ah yes, still haven’t figured out why you’d want to use commands instead of on-click type binding.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s