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

编程入门 行业动态 更新时间:2024-10-27 06:26:51
本文介绍了在WPF中如何添加菜单项事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的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中如何添加菜单项事件?

本文发布于:2023-11-10 22:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1576696.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:菜单项   事件   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!