How to programmatically click a button in WPF?

前端 未结 8 945
情深已故
情深已故 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:10

    As Greg D said, I think that an alternative to Automation to click a button using the MVVM pattern (click event raised and command executed) is to call the OnClick method using reflection:

    typeof(System.Windows.Controls.Primitives.ButtonBase).GetMethod("OnClick", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(button, new object[0]);
    

提交回复
热议问题