1

How can one program a program close ("Exit") item on a menu? So to close the wpf window when clicked?

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
user3824825
  • 53
  • 1
  • 2
  • 9
  • Do you actually mean `winforms` by `wpf`? I would like to re-edit your question (replace the `wpf` with `winforms` and re-tag it). – King King Jul 10 '14 at 10:00
  • That's not the same question as the one linked as a reason for closing this question. Although this question overlaps the topic in the linked question. – Guillaume Apr 14 '20 at 20:58

1 Answers1

15

WPF comes has a control which is Menu. One adds MenuItem elements to a Menu, and each MenuItem can have a range of sub-items, hence allowing for the creation of hierarchical menus.

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();
 }
Community
  • 1
  • 1
MRebai
  • 5,344
  • 3
  • 33
  • 52