Files
docs-desktop/dotnet-desktop-guide/framework/wpf/controls/menu-overview.md
T
Andy De George da363692ff Initial WPF content migrated (#17)
* Reset branch for WPF changes

* Convert BMP to PNG; fix link-out-of-scope err

* Add snippets for WPF... 6794 files!!!!

* Add missing snippets

* update file updated between migration

* Fix paths to include

* update breadcrumb and toc

* fix index links

* fix index links

* fix index links

* fix markdown
2020-09-04 09:46:28 -07:00

5.4 KiB

title, ms.date, helpviewer_keywords, ms.assetid
title ms.date helpviewer_keywords ms.assetid
Menu Overview 03/30/2017
Menu control [WPF]
controls [WPF], Menu
67df6de5-db96-4c71-b752-af90729a6537

Menu Overview

The xref:System.Windows.Controls.Menu class enables you to organize elements associated with commands and event handlers in a hierarchical order. Each xref:System.Windows.Controls.Menu element contains a collection of xref:System.Windows.Controls.MenuItem elements.

Menu Control

The xref:System.Windows.Controls.Menu control presents a list of items that specify commands or options for an application. Typically, clicking a xref:System.Windows.Controls.MenuItem opens a submenu or causes an application to carry out a command.

Creating Menus

The following example creates a xref:System.Windows.Controls.Menu to manipulate text in a xref:System.Windows.Controls.TextBox. The xref:System.Windows.Controls.Menu contains xref:System.Windows.Controls.MenuItem objects that use the xref:System.Windows.Controls.MenuItem.Command%2A, xref:System.Windows.Controls.MenuItem.IsCheckable%2A, and xref:System.Windows.Controls.HeaderedItemsControl.Header%2A properties and the xref:System.Windows.Controls.MenuItem.Checked, xref:System.Windows.Controls.MenuItem.Unchecked, and xref:System.Windows.Controls.MenuItem.Click events.

[!code-xamlMenuItemCommandsAndEvents#1]

[!code-csharpMenuItemCommandsAndEvents#2] [!code-vbMenuItemCommandsAndEvents#2]

MenuItems with Keyboard Shortcuts

Keyboard shortcuts are character combinations that can be entered with the keyboard to invoke xref:System.Windows.Controls.Menu commands. For example, the shortcut for Copy is CTRL+C. There are two properties to use with keyboard shortcuts and menu items —xref:System.Windows.Controls.MenuItem.InputGestureText%2A or xref:System.Windows.Controls.MenuItem.Command%2A.

InputGestureText

The following example shows how to use the xref:System.Windows.Controls.MenuItem.InputGestureText%2A property to assign keyboard shortcut text to xref:System.Windows.Controls.MenuItem controls. This only places the keyboard shortcut in the menu item. It does not associate the command with the xref:System.Windows.Controls.MenuItem. The application must handle the user's input to carry out the action.

[!code-xamlMenuEvent#6]

Command

The following example shows how to use the xref:System.Windows.Controls.MenuItem.Command%2A property to associate the Open and Save commands with xref:System.Windows.Controls.MenuItem controls. Not only does the command property associate a command with a xref:System.Windows.Controls.MenuItem, but it also supplies the input gesture text to use as a shortcut.

[!code-xamlMenuEvent#8]

The xref:System.Windows.Controls.MenuItem class also has a xref:System.Windows.Controls.MenuItem.CommandTarget%2A property, which specifies the element where the command occurs. If xref:System.Windows.Controls.MenuItem.CommandTarget%2A is not set, the element that has keyboard focus receives the command. For more information about commands, see Commanding Overview.

Menu Styling

With control styling, you can dramatically change the appearance and behavior of xref:System.Windows.Controls.Menu controls without having to write a custom control. In addition to setting visual properties, you can also apply a xref:System.Windows.Style 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 xref:System.Windows.Style to a xref:System.Windows.Controls.Menu control.

The first code example defines a xref:System.Windows.Style 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-xamlMenuStylesSnippet#1]

The following sample uses xref:System.Windows.Trigger elements that enable you to change the appearance of a xref:System.Windows.Controls.MenuItem in response to events that occur on the xref:System.Windows.Controls.Menu. When you move the mouse over the xref:System.Windows.Controls.Menu, the foreground color and the font characteristics of the menu items change.

[!code-xamlMenuStylesSnippet#2]

See also