共用方式為


MenuAction 類別

更新:2007 年 11 月

表示要在設計工具中採用之動作的內容功能表項目。

命名空間:  Microsoft.Windows.Design.Interaction
組件:  Microsoft.Windows.Design.Extensibility (在 Microsoft.Windows.Design.Extensibility.dll 中)

語法

Public Class MenuAction _
    Inherits MenuBase

Dim instance As MenuAction
public class MenuAction : MenuBase
public ref class MenuAction : public MenuBase
public class MenuAction extends MenuBase

備註

使用 MenuAction 類別,可以定義 WPF 設計工具中的內容功能表項目。 

若要顯示內容功能表項目,請繼承自 ContextMenuProvider 類別,並建立 MenuAction 項目與相關聯的 MenuGroup。這些功能表物件通常是在衍生自 PrimarySelectionContextMenuProvider (即在主要選取上顯示內容功能表) 之類別的建構函式 (Constructor) 中建立。

您可以在 Execute 事件處理常式中實作 MenuAction 的邏輯。

MenuAction 類別與 WPF 命令系統相容。請使用 Command 屬性,以程式設計的方式叫用 MenuAction,而不透過使用者介面。

範例

在下列程式碼範例中,會說明如何設定兩個 MenuAction 項目,這兩個項目會在設計期間設定控制項的 Background 屬性。如需詳細資訊,請參閱逐步解說:建立 MenuAction

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);
}

繼承階層架構

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

執行緒安全

這個型別的任何 Public static (在 Visual Basic 中為 Shared) 成員都具備執行緒安全。並非所有的執行個體成員都是安全執行緒。

請參閱

參考

MenuAction 成員

Microsoft.Windows.Design.Interaction 命名空間

PrimarySelectionContextMenuProvider

MenuGroup

其他資源

逐步解說:建立 MenuAction