MenuItem 类

用于创建和修改 Web 部件菜单上的项的行为。

继承层次结构

System.Object
  Microsoft.SharePoint.WebPartPages.MenuItem

命名空间:  Microsoft.SharePoint.WebPartPages
程序集:  Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)

语法

声明
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public Class MenuItem
用法
Dim instance As MenuItem
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class MenuItem

示例

下面的代码示例显示包含两个自定义的布尔属性、 ParentItemIsVisible和EnableItem1,允许您控制这些菜单项时都能看到并启用重写的CreateWebPartMenu 。它还假定此方法是包含名为EventHandlerForItem1事件处理程序的 Web 部件类的一部分。

Public Overrides Sub CreateWebPartMenu()
    ' Declare variables for menu items.
    Dim ParentItem As MenuItem
    Dim Item1 As MenuItem
    Dim Item2 As MenuItem
    
    ' Create three menu items:
    ' One parent item, and two submenu items.
    ' Create the parent item.
    ParentItem = New MenuItem("ParentItem", "", "ParentItemID")
    
    ' Create a submenu item with a server event on click.
    Item1 = New MenuItem("Item1", "Item1ID", New EventHandler(AddressOf EventHandlerForItem1))
    
    ' Create a submenu item with a client event on click. 
    Item2 = New MenuItem("Item2", "javascript:alert('Item2 was clicked.');", "Item2ID")
    
    ' Add the submenu items to the parent item.
    ParentItem.MenuItems.Add(Item1)
    ParentItem.MenuItems.Add(Item2)
   
    ' Add the parent item after the "Modify Shared/Personal Web Part"
    ' command in the default menu. 
    ' Retrieve the index of the "Modify Shared/Personal Web Part" 
    ' command.
    Dim EditIndex As Integer = Me.WebPartMenu.MenuItems.IndexOf(Me.WebPartMenu.MenuItems.ItemFromID("MSOMenu_Edit"))
    
    ' Insert the parent item after the "Modify Shared/Personal Web 
    ' Part" command.
    Me.WebPartMenu.MenuItems.Insert(EditIndex + 1, ParentItem)
     
    ' Add a separator above the parent item.
    ParentItem.BeginSection = True
         
    ' Check the "ParentItemIsVisible" custom boolean property to decide 
    ' whether to display the parent menu.
    If Me.ParentItemIsVisible = True Then
       ParentItem.Visible = True
         
    Else
       ParentItem.Visible = False
    End If
         
    ' Check the "EnableItem1" custom boolean property to decide which 
    ' menu item to enable. 
    ' within the parent item.
    If Me.EnableItem1 = True Then
       Item1.Enabled = True
       Item2.Enabled = False
         
    Else
       Item1.Enabled = False
       Item2.Enabled = True
    End If
End Sub
public override void CreateWebPartMenu()
{
    // Declare variables for menu items.
    MenuItem ParentItem;
    MenuItem Item1;
    MenuItem Item2;

    // Create three menu items:
    // One parent item, and two submenu items.

    // Create the parent item.
    ParentItem = new MenuItem("ParentItem", "", "ParentItemID");

    // Create a submenu item with a server event on click.
    Item1 = new MenuItem("Item1", "Item1ID", new EventHandler(EventHandlerForItem1));

    // Create a submenu item with a client event on click. 
    Item2 = new MenuItem("Item2", "javascript:alert('Item2 was clicked.');", "Item2ID");

    // Add the submenu items to the parent item.
    ParentItem.MenuItems.Add(Item1);
    ParentItem.MenuItems.Add(Item2);

    // Add the parent item after the "Modify Shared/Personal Web Part"
    // command in the default menu. 

    // Retrieve the index of the "Modify Shared/Personal Web Part" 
    // command.
    int EditIndex = this.WebPartMenu.MenuItems.IndexOf(this.WebPartMenu.MenuItems.ItemFromID("MSOMenu_Edit"));

    // Insert the parent item after the "Modify Shared/Personal Web 
    // Part" command.
    this.WebPartMenu.MenuItems.Insert(EditIndex + 1, ParentItem); 

    // Add a separator above the parent item.
    ParentItem.BeginSection = true;

    // Check the "ParentItemIsVisible" custom boolean property to 
    // decide whether to display the parent menu.
    if (this.ParentItemIsVisible == true)
    {
        ParentItem.Visible = true;
    }

    else
    {
        ParentItem.Visible = false;
    }

    // Check the "EnableItem1" custom boolean property to decide which 
    // menu item to enable. 
    // within the parent item.
    if (this.EnableItem1 == true)
    {
        Item1.Enabled=true;
        Item2.Enabled=false;
    }
    
    else
    {
        Item1.Enabled=false;
        Item2.Enabled=true;
    }
}

线程安全性

该类型的任何公共 静态 (已共享 在 Visual Basic 中) 成员都是线程安全的。不保证任何实例成员都是线程安全的。

另请参阅

引用

MenuItem 成员

Microsoft.SharePoint.WebPartPages 命名空间