--- title: "Menu Overview" ms.date: "03/30/2017" helpviewer_keywords: - "Menu control [WPF]" - "controls [WPF], Menu" ms.assetid: 67df6de5-db96-4c71-b752-af90729a6537 --- # Menu Overview The class enables you to organize elements associated with commands and event handlers in a hierarchical order. Each element contains a collection of elements. ## Menu Control The control presents a list of items that specify commands or options for an application. Typically, clicking a opens a submenu or causes an application to carry out a command. ## Creating Menus The following example creates a to manipulate text in a . The contains objects that use the , , and properties and the , , and events. [!code-xaml[MenuItemCommandsAndEvents#1](~/samples/snippets/csharp/VS_Snippets_Wpf/MenuItemCommandsAndEvents/CSharp/Window1.xaml#1)] [!code-csharp[MenuItemCommandsAndEvents#2](~/samples/snippets/csharp/VS_Snippets_Wpf/MenuItemCommandsAndEvents/CSharp/Window1.xaml.cs#2)] [!code-vb[MenuItemCommandsAndEvents#2](~/samples/snippets/visualbasic/VS_Snippets_Wpf/MenuItemCommandsAndEvents/VisualBasic/Window1.xaml.vb#2)] ## MenuItems with Keyboard Shortcuts Keyboard shortcuts are character combinations that can be entered with the keyboard to invoke commands. For example, the shortcut for **Copy** is CTRL+C. There are two properties to use with keyboard shortcuts and menu items — or . ### InputGestureText The following example shows how to use the property to assign keyboard shortcut text to controls. This only places the keyboard shortcut in the menu item. It does not associate the command with the . The application must handle the user's input to carry out the action. [!code-xaml[MenuEvent#6](~/samples/snippets/csharp/VS_Snippets_Wpf/MenuEvent/CSharp/Pane1.xaml#6)] ### Command The following example shows how to use the property to associate the **Open** and **Save** commands with controls. Not only does the command property associate a command with a , but it also supplies the input gesture text to use as a shortcut. [!code-xaml[MenuEvent#8](~/samples/snippets/csharp/VS_Snippets_Wpf/MenuEvent/CSharp/Pane1.xaml#8)] The class also has a property, which specifies the element where the command occurs. If is not set, the element that has keyboard focus receives the command. For more information about commands, see [Commanding Overview](../advanced/commanding-overview.md). ## Menu Styling With control styling, you can dramatically change the appearance and behavior of controls without having to write a custom control. In addition to setting visual properties, you can also apply a to individual parts of a control, change the behavior of parts of the control through properties, or add additional parts or change the layout of a control. The following examples demonstrate several ways to add a to a control. The first code example defines a called `Simple` that shows how to use the current system settings in your style. The code assigns the color of the `MenuHighlightBrush` as the menu's background color and the `MenuTextBrush` as the menu's foreground color. Notice that you use resource keys to assign the brushes. [!code-xaml[MenuStylesSnippet#1](~/samples/snippets/csharp/VS_Snippets_Wpf/MenuStylesSnippet/CS/app.xaml#1)] The following sample uses elements that enable you to change the appearance of a in response to events that occur on the . When you move the mouse over the , the foreground color and the font characteristics of the menu items change. [!code-xaml[MenuStylesSnippet#2](~/samples/snippets/csharp/VS_Snippets_Wpf/MenuStylesSnippet/CS/app.xaml#2)] ## See also - [WPF Controls Gallery Sample](https://github.com/Microsoft/WPF-Samples/tree/master/Getting%20Started/ControlsAndLayout)