In WPF how to add menu item event?

纵饮孤独 提交于 2019-12-19 05:17:03

问题


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(e.g. 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!


回答1:


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)
{

}


来源:https://stackoverflow.com/questions/5479151/in-wpf-how-to-add-menu-item-event

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!