参考网上方法,整理记录了两种方法。以Button按钮为例:
- 1直接调用Button Click方法,参数传入null
MyButton_Click(null, null)
- 2使用RaiseEvent方法
//msdn中不推荐在代码中使用Mouse.PrimaryDevice。
//原文:This property supports the Windows Presentation Foundation infrastructure and is not intended to be used directly from your code.
MouseButtonEventArgs args = new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left);
args.RoutedEvent = Button.ClickEvent;
MyButton.RaiseEvent(args);
根据MSDN可以这样使用RaiseEvent
RoutedEventArgs eventArgs = new RoutedEventArgs(Button.ClickEvent);
MyButton.RaiseEvent(eventArgs);