在Excel 2013中dynamic禁用/启用自定义上下文菜单
我有一个Excel AddIn。 我在单元格的上下文菜单中添加2个上下文菜单项。
当您右键单击一个单元格时,根据单元格的公式,一个上下文菜单项将被禁用。
我把这个代码放在sheetSelectionChangeEvent
这在Excel 2003,2007和2010中正常工作,但在Excel 2013中不起作用。
以下是代码:
private void ApplicationSheetSelectionChange(COMObject sh, Range target) { DisableMenubarsButtonsWRibbon(XLApp.Selection as Range); } public void DisableMenubarsButtonsWRibbon(Range rng) { var formula = rng.Formula as string; if(formula is function1) { _contextMenuItem1.Enabled = true; _contextMenuItem2.Enabled = false; } else if(formula is function2) { _contextMenuItem1.Enabled = false; _contextMenuItem2.Enabled = true; } else { _contextMenuItem1.Enabled = true; _contextMenuItem2.Enabled = true; } }
您报告的行为的原因:
您报告的行为是由于MS Excel 2013核心的错误 。
行为不是由于创buildExcel加载项的libriaries /工具包:
- 当您将问题报告给ExcelDNA和NetOffice时,我遇到了与VSTO 2010相同的问题
- 在这两种情况下,启用上下文菜单项都可以很好地与Office 2010配合使用,并停止使用Office 2013。
我已经在MS Connect上提交了Visual Studio和.NET Framework区域的缺陷,因为我没有find他们接受Office缺陷的地方。
就像在MS Connect上一些时候缺陷的链接出现了问题一样 – 我将在这个答案的末尾添加缺陷的细节和截图。
解决方法 – 启用禁用菜单项
正如您在MSDN社区论坛上回答您的问题所述 – 您可以通过Caption
或Tag
属性检索项目时控制上下文菜单项的Enabled
状态:
CommandBar contextMenu = Globals.ThisAddIn.Application.CommandBars["Cell"]; foreach (CommandBarControl control in contextMenu.Controls) { if (control.Caption == "item caption") // Specify here caption of your context menu item { contextMenuItem = (CommandBarButton)control; contextMenuItem.Enabled = true; // Specify here desired value for Enabled state to be set } }
在上面的代码中,剪切上下文菜单项是通过Caption
find的 – 通过Tag
查找项目replace代码:
control.Caption == "item caption"
if
条件与:
control.Tag == "item tag"
指定适当的值而不是item tag
文本
缺陷细节
MS Connect门票有ID 813453和标题:
对于使用C#.NET和Visual Studio 2012创build的Excel 2013加载项的上下文菜单项,启用状态不会更改
以下是门票的截图: