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));

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 to timscyclingblog Cancel reply