在Word 2007 Addin中启用/禁用功能区按钮

时间:2011-04-25 14:06:37

标签: c# vsto

目前我正在使用Addin这个词,我已经动态地向功能区添加了控件。现在,我需要捕获动态按钮“btnSubmit”并根据我需要启用/禁用按钮的条件。

首次打开文档时应该启用它,单击按钮后应该禁用它。

这应该在布尔条件下完成。任何帮助将不胜感激。

感谢, KSR Prasad

3 个答案:

答案 0 :(得分:1)

可以通过RibbonXML使用getEnabled事件。

功能区XML:

<button id="button1" onAction="button1_Click" getEnabled="button1_EnabledChanged" />

代码:

public void button1_Click(Office.IRibbonControl control)
{
    if (control.Id == "button1")
    {
        // your code
        clicked = true; // a boolean variable
    }
}

public bool button1_EnabledChanged(Office.IRibbonControl control)
{
    if (control.Id == "button1")
        return !clicked;
}

答案 1 :(得分:0)

如果您已经创建了按钮,只需创建一个区域范围WITHEVENTS变量来保存它,分配它,然后对click事件做出反应以禁用该按钮(按钮对象具有启用的属性)。

Private WithEvents _MyButton As Ribbon.RibbonButton
....
Set _MyButton = {the just created button}

然后处理点击事件

答案 2 :(得分:0)

我对此类问题的偏好是使用RibbonXml而不是设计器。

一个非常简单的选项是有一个Dictionary,然后你可以存储在Ribbon回调类中。如果您想要一个更好的选项,VSTO Contrib(http://vstocontrib.codeplex.com/)允许您非常轻松地为每个文档创建一个“viewmodel”,然后您只需将启用的按钮绑定到viewmodel上的属性即可。

有关功能区xml的更多信息:http://jake.ginnivan.net/vsto-ribbon-designer-in-depth
有关vsto contrib的更多信息以及它如何为您提供帮助:http://jake.ginnivan.net/vsto-contrib/ribbon-factory

干杯,
杰克