问题描述
在我的WPF应用程序中,我添加一个菜单,然后在它下面添加几个菜单项。例如,我的一个菜单项是Main Item,然后在Main Item下添加subItem1,subItem2和subItem3。我想点击subItem1并做一些事情(例如MessageBox.show一个消息)。为什么我找不到此subItem1的事件?如何为subItem1添加单击事件?我在Main Item的集合属性下找到subItem1的属性,但是只能看到属性,看不到事件列表。如何为subItem1添加点击事件?谢谢!
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!
推荐答案
在您的xaml中:
<Menu IsMainMenu="True">
<MenuItem Header="MainMenu">
<MenuItem Header="subItem1"
x:Name="subItem1" Click="subItem1_Click">
</MenuItem>
</MenuItem>
</Menu>
在您的代码隐藏中:
private void subItem1_Click(object sender, RoutedEventArgs e)
{
}
这篇关于在WPF中如何添加菜单项事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!