管理 GridDesktops 上下文菜单

介绍

ContextMenuManager 类用于管理上下文菜单项。 GridDesktop.ContextMenuManager 属性获取 ContextMenuManager 对象的实例。例如,ContextMenuManager.MenuItemAvailable_Copy 属性获取或设置一个值,该值指示上下文菜单项 Copy 是否可用。同样,我们拥有不同上下文菜单项的所有相应属性。

**重要的:**默认情况下,所有上下文菜单项都在列表中可见。

管理上下文菜单

隐藏上下文菜单项

要执行此任务,我们首先看一下 GridDesktop 的默认上下文菜单。

GridDeskop 的默认菜单

待办事项:图片_替代_文本

现在,使用以下代码隐藏一些菜单项:

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Get the ContextMenuManager
ContextMenuManager cmm = this.grdDataEntry.ContextMenuManager;
// Hide the Copy option in the context menu
cmm.MenuItemAvailable_Copy = false;
// Hide the InsertRow option in the context menu
cmm.MenuItemAvailable_InsertRow = false;
// Hide the Format Cell dialog box
cmm.MenuItemAvailable_FormatCells = false;

执行上述代码后,一些菜单项将对用户不可见:

一些菜单项被隐藏

待办事项:图片_替代_文本

添加新菜单项

使用以下代码片段将新的上下文菜单项添加到列表中。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Get the active worksheet
Worksheet sheet = grdDataEntry.GetActiveWorksheet();
// Set the total columns diaplyed in the grid
sheet.ColumnsCount = 15;
// Set the total rows displayed in the grid
sheet.RowsCount = 15;
// Define a new menu item and specify its event handler
MenuItem mi = new MenuItem("newMenuItem", new System.EventHandler(miClicked));
// Set the label
mi.Text = "New Item";
// Add the menu item to the GridDesktop's context menu
grdDataEntry.ContextMenu.MenuItems.Add(mi);

我们还为新命令/选项指定一个事件处理程序。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Event Handler for the new menu item
private void miClicked(object sender, EventArgs e)
{
MenuItem mi = (MenuItem)sender;
MessageBox.Show("miCliked: " + mi.Text);
}

执行上述代码后,可以在上下文菜单中看到一个新的菜单项。单击单元格时也会出现一条消息。

一个新的菜单项被添加到列表中

待办事项:图片_替代_文本