wpf 在WPF中如何添加菜单项事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5479151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:43:16  来源:igfitidea点击:

In WPF how to add menu item event?

wpfeventsmenuclick

提问by spspli

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!

在我的 WPF 应用程序中,我添加了一个菜单,然后在它下面添加了几个菜单项。例如,我的菜单项之一是“Main Item”,然后我在“Main Item”下添加 subItem1、subItem2 和 subItem3。我想单击 subItem1 并执行某些操作(例如 MessageBox.show a message)。为什么我找不到这个 subItem1 的事件?如何为 subItem1 添加点击事件?我在“主项目”的集合属性下找到了 subItem1 的属性,但只能看到属性,无法看到事件列表。如何为 subItem1 添加点击事件?谢谢!

回答by dugas

In your xaml:

在您的 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)
{

}