Command Property

MenuItem.Command Property

Gets or sets the command associated with the menu item. This is a dependency property.

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)

[LocalizabilityAttribute(LocalizationCategory.NeverLocalize)]
[BindableAttribute(true)]
public ICommand Command { get; set; }
For XAML information, see the ICommand type.

Property Value

Type: System.Windows.Input.ICommand
The command associated with the MenuItem. The default is null.

Implements

ICommandSource.Command

Identifier field

CommandProperty

Metadata properties set to true

None

This property is used to associate a command with a particular MenuItem instance. The following example associates the Copy command with a MenuItem and automatically supplies the input gesture text Ctrl+C. The example does not set the Header property, but the Header for the MenuItem is "Copy" at run time. You can specify another header for the MenuItem if you want it to be different. For information on how to bind to the desired command, see How to: Add a Command to a MenuItem.

<MenuItem Command="ApplicationCommands.Copy"/>

A command is raised when a MenuItem is clicked—just after the Click event. The command is raised on the element according to the following priorities:

  1. If CommandTarget is set on the MenuItem, that element is used.

  2. The PlacementTarget of a ContextMenu that contains the MenuItem.

  3. The focus target of the main window that contains a Menu.

  4. The MenuItem that was clicked.

The following examples show how to use the command property to associate commands with menu items.

<MenuItem Header="_Open" Command="ApplicationCommands.Open"/>
<MenuItem Header="_Save" Command="ApplicationCommands.Save"/>
mib = new MenuItem();
mib.Command = System.Windows.Input.ApplicationCommands.Copy;
mib.Header = "_Copy";
mi.Items.Add(mib);

mic = new MenuItem();
mic.Command = System.Windows.Input.ApplicationCommands.Paste;
mic.Header = "_Paste";
mi.Items.Add(mic);

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0

Community Additions

ADD
Show:
© 2016 Microsoft