How to programmatically click a button in WPF?

前端 未结 8 914
情深已故
情深已故 2020-11-28 02:00

Since there\'s no button.PerformClick() method in WPF, is there a way to click a WPF button programmatically?

相关标签:
8条回答
  • 2020-11-28 02:17

    Like JaredPar said you can refer to Josh Smith's article towards Automation. However if you look through comments to his article you will find more elegant way of raising events against WPF controls

    someButton.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));
    

    I personally prefer the one above instead of automation peers.

    0 讨论(0)
  • 2020-11-28 02:29

    One way to programmatically "click" the button, if you have access to the source, is to simply call the button's OnClick event handler (or Execute the ICommand associated with the button, if you're doing things in the more WPF-y manner).

    Why are you doing this? Are you doing some sort of automated testing, for example, or trying to perform the same action that the button performs from a different section of code?

    0 讨论(0)
提交回复
热议问题