简体   繁体   中英

In WPF how to add menu item event?

In my WPF application, I add a menu then add several menu items under it. For example, one of my menu item is "Main Item", then I add subItem1, subItem2 and subItem3 under "Main Item". I want to click subItem1 and do something(eg MessageBox.show a message). Why I cannot find the event for this subItem1? How can I add the click event for subItem1? I find the property for subItem1 under the collection property for "Main Item", but can only see property, cannot see event list. How can I add click event for subItem1? Thank you!

In your xaml:

<Menu IsMainMenu="True">
<MenuItem Header="MainMenu">
<MenuItem Header="subItem1" 
 x:Name="subItem1" Click="subItem1_Click">
</MenuItem>
</MenuItem>
</Menu>

In your code-behind:

private void subItem1_Click(object sender, RoutedEventArgs e)
{

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM