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

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

How to Provide an "Exit" Menu Item

c#wpfmenuitemexit

提问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();
 }