How do I listen to click events on all the MenuItems through the parent ContextMenu?

Platform: WPF| Category: Context Menu

Instead of setting up event handlers on all the MenuItems you can listen to those events as they bubble up to the context menu as follows:

[XAML]

            MenuItem1
            MenuItem2
            MenuItem3
            MenuItem4

Or you can setup the same thing in code as follows:

[C#]
menuItem1.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(MnItem_Click));

The event handler would then be something like:


private void MnItem_Click(object sender, RoutedEventArgs e)
{
	MenuItem item = e.Source as MenuItem;
            
	....
}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.