wpf 如何提供“退出”菜单项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24673207/
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
How to Provide an "Exit" Menu Item
提问by user3824825
How can one program a program close ("Exit") item on a menu? So to close the wpf window when clicked?
如何编程一个程序关闭(“退出”)菜单上的项目?那么点击时关闭wpf窗口?
回答by Moez Rebai
WPF comes has a control which is Menu. One adds MenuItemelements to a Menu, and each MenuItemcan have a range of sub-items, hence allowing for the creation of hierarchical menus.
WPF 自带一个控件,它是Menu. 一个MenuItem向 a添加元素Menu,每个元素都MenuItem可以有一系列子项,因此允许创建分层菜单。
Example
例子
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File">
<MenuItem Header="_New" />
<MenuItem Header="_Open" />
<MenuItem Header="_Save" />
<Separator />
<MenuItem Header="_Exit" Click="MenuItem_Click"/>
</MenuItem>
</Menu>
<TextBox AcceptsReturn="True" />
</DockPanel>
Code Behind
背后的代码
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}

