MenuAction Class

A context menu item which represents an action to take in the designer.

Inheritance Hierarchy

System.Object
  Microsoft.Windows.Design.Interaction.MenuBase
    Microsoft.Windows.Design.Interaction.MenuAction

Namespace:  Microsoft.Windows.Design.Interaction
Assembly:  Microsoft.Windows.Design.Interaction (in Microsoft.Windows.Design.Interaction.dll)

Syntax

'Declaration
Public Class MenuAction _
    Inherits MenuBase
public class MenuAction : MenuBase
public ref class MenuAction : public MenuBase
type MenuAction =  
    class
        inherit MenuBase
    end
public class MenuAction extends MenuBase

The MenuAction type exposes the following members.

Constructors

  Name Description
Public method MenuAction Initializes a new instance of the MenuAction class.

Top

Properties

  Name Description
Public property Checkable Gets or sets a value indicating whether the menu item requires user interface (UI) that displays a check box.
Public property Checked Gets or sets a value indicating whether the menu item is checked.
Public property Command Gets the command which represents the menu action.
Public property Context Gets the current editing context. (Inherited from MenuBase.)
Public property DisplayName Gets or sets the localized text to display for the menu item. (Inherited from MenuBase.)
Public property Enabled Gets or sets a value indicating whether the menu action item is available to the user.
Public property ImageUri Gets or sets the path to an image associated with the MenuAction.
Public property Name Gets or sets the unique identifier for the menu item. (Inherited from MenuBase.)
Public property Visible Gets or sets a value indicating whether the item is displayed in the menu.

Top

Methods

  Name Description
Public method Equals Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Protected method OnPropertyChanged Raises the PropertyChanged event. (Inherited from MenuBase.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Events

  Name Description
Public event Execute Occurs when the menu item is executed.
Public event PropertyChanged Occurs when a property has changed. (Inherited from MenuBase.)

Top

Remarks

Use the MenuAction class to define a context menu item in the WPF Designer. 

To show context menu items, inherit from the ContextMenuProvider class and create MenuAction items and an associated MenuGroup. These menu objects are usually created in the constructor of a class derived from PrimarySelectionContextMenuProvider, which shows the context menu on the primary selection.

Implement the logic for your MenuAction in the Execute event handler.

The MenuAction class is compatible with the WPF command system. Use the Command property to invoke the MenuAction programmatically, instead of through the user interface.

Examples

The following code example shows how to set up two MenuAction items, which set the Background property of a control at design time. For more information, see Walkthrough: Creating a Menu Provider.

Private setBackgroundToBlueMenuAction As MenuAction
Private clearBackgroundMenuAction As MenuAction


...


' The provider's constructor sets up the MenuAction objects 
' and the the MenuGroup which holds them.
Public Sub New()

    ' Set up the MenuAction which sets the control's 
    ' background to Blue.
    setBackgroundToBlueMenuAction = New MenuAction("Blue")
    setBackgroundToBlueMenuAction.Checkable = True
    AddHandler setBackgroundToBlueMenuAction.Execute, AddressOf SetBackgroundToBlue_Execute

    ' Set up the MenuAction which sets the control's 
    ' background to its default value.
    clearBackgroundMenuAction = New MenuAction("Cleared")
    clearBackgroundMenuAction.Checkable = True
    AddHandler clearBackgroundMenuAction.Execute, AddressOf ClearBackground_Execute

    ' Set up the MenuGroup which holds the MenuAction items.
    Dim backgroundFlyoutGroup As New MenuGroup("SetBackgroundsGroup", "Set Background")

    ' If HasDropDown is false, the group appears inline, 
    ' instead of as a flyout. Set to true.
    backgroundFlyoutGroup.HasDropDown = True
    backgroundFlyoutGroup.Items.Add(setBackgroundToBlueMenuAction)
    backgroundFlyoutGroup.Items.Add(clearBackgroundMenuAction)
    Me.Items.Add(backgroundFlyoutGroup)

    ' The UpdateItemStatus event is raised immediately before 
    ' this provider shows its tabs, which provides the opportunity 
    ' to set states.
    AddHandler UpdateItemStatus, AddressOf CustomContextMenuProvider_UpdateItemStatus

End Sub
private MenuAction setBackgroundToBlueMenuAction;
private MenuAction clearBackgroundMenuAction;


...


// The provider's constructor sets up the MenuAction objects 
// and the the MenuGroup which holds them.
public CustomContextMenuProvider()
{   
    // Set up the MenuAction which sets the control's 
    // background to Blue.
    setBackgroundToBlueMenuAction = new MenuAction("Blue");
    setBackgroundToBlueMenuAction.Checkable = true;
    setBackgroundToBlueMenuAction.Execute += 
        new EventHandler<MenuActionEventArgs>(SetBackgroundToBlue_Execute);

    // Set up the MenuAction which sets the control's 
    // background to its default value.
    clearBackgroundMenuAction = new MenuAction("Cleared");
    clearBackgroundMenuAction.Checkable = true;
    clearBackgroundMenuAction.Execute += 
        new EventHandler<MenuActionEventArgs>(ClearBackground_Execute);

    // Set up the MenuGroup which holds the MenuAction items.
    MenuGroup backgroundFlyoutGroup = 
        new MenuGroup("SetBackgroundsGroup", "Set Background");

    // If HasDropDown is false, the group appears inline, 
    // instead of as a flyout. Set to true.
    backgroundFlyoutGroup.HasDropDown = true;
    backgroundFlyoutGroup.Items.Add(setBackgroundToBlueMenuAction);
    backgroundFlyoutGroup.Items.Add(clearBackgroundMenuAction);
    this.Items.Add(backgroundFlyoutGroup);

    // The UpdateItemStatus event is raised immediately before 
    // this provider shows its tabs, which provides the opportunity 
    // to set states.
    UpdateItemStatus += 
        new EventHandler<MenuActionEventArgs>(
            CustomContextMenuProvider_UpdateItemStatus);
}

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

Microsoft.Windows.Design.Interaction Namespace

PrimarySelectionContextMenuProvider

MenuGroup

Other Resources

Walkthrough: Creating a Menu Provider