管理 GridDesktop 上下文菜单

介绍

ContextMenuManager 类用于管理上下文菜单项。GridDesktop.ContextMenuManager 属性获取 ContextMenuManager 对象的实例。例如,ContextMenuManager.MenuItemAvailable_Copy 属性获取或设置一个值,表示上下文菜单项“拷贝”是否可用。类似地,我们还有不同上下文菜单项的相应属性。

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

管理上下文菜单

隐藏上下文菜单项

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

GridDesktop 的默认菜单

todo:image_alt_text

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

// 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;

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

部分菜单项被隐藏

todo:image_alt_text

添加新菜单项

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

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

执行上述代码后,上下文菜单中将出现一个新的菜单项。单击单元格时还将出现一条消息。

列表中添加了一个新菜单项

todo:image_alt_text