How to invoke click event in WPF programmatically

Sometimes you need to to trigger a click or some other event for one of your WPF controls, such as a button, a menu or a checkbox directly from code. There are no methods on the controls themselves do do this directly per event type. However it is not a big deal anyway - simply use RaiseEvent like this (assuming you have a MenuItem object called item):

item.RaiseEvent(new RoutedEventArgs(MenuItem.ClickEvent));

That's it. The only thing to be careful with is calling RaiseEvent from inside another event handler for the object.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
Please confirm you are a human being
Image CAPTCHA
Enter the characters shown in the image.