VSTO:如何从ThisAddIn类中设置功能区的按钮

时间:2014-05-11 01:25:24

标签: c# class vsto ribbon

我有一个功能区,里面有一个按钮。 当用户选择新文档时,我想更改功能区的按钮文本。 我想我已经完成了所有工作,只有一行:

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        var wb = this.Application;
        ((Word.ApplicationEvents4_Event)wb).NewDocument += new Word.ApplicationEvents4_NewDocumentEventHandler(Application_NewDocument);

    }
    /// <summary>
    /// When the new button is pressed, the save should be enabled
    /// </summary>
    /// <param name="Doc"></param>
    void Application_NewDocument(Microsoft.Office.Interop.Word.Document Doc)
    {
    // set button's properties here
    }

2 个答案:

答案 0 :(得分:3)

最简单的方法是将事件处理程序放在Ribbon类中。然后你可以简单地使用:

this.Button.Label = "text";

否则,您必须创建一个将其封送到外部世界的属性:

功能区:

public string ButtonText {get {return this.Button.Label;} set {this.Button.Label = value;} }

在ThisAddIn中:

Globals.Ribbons.Ribbon.ButtonText = "text";

答案 1 :(得分:1)

您需要调用功能区回调事件&#34; getlabel&#34;设置文本。在Newdocument事件中设置变量名称,如下面的示例代码所示,然后调用功能区InvalidateControl,它将重新绘制功能区并执行回调。

<强> Ribbon.xml

<?xml version="1.0" encoding="UTF-8"?>
<customUI onLoad="Ribbon_Load" xmlns="http://schemas.microsoft.com/office/2006/01/customui">
  <ribbon>
    <tabs>
      <tab idMso="TabAddIns" label="3rd Party Add-ins" />      
          <button id="btnTest" onAction="btnTest_Click" getLabel="get_LabelName" size="large" />        
      </tab>
    </tabs>
  </ribbon>
</customUI>

<强> Ribbon.cs

public class Ribbon : IRibbonExtensibility
{
    private static IRibbonUI ribbon;
    private string buttonText;
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        var wb = this.Application;
        ((Word.ApplicationEvents4_Event)wb).NewDocument += new Word.ApplicationEvents4_NewDocumentEventHandler(Application_NewDocument);

    }
    /// <summary>
    /// When the new button is pressed, the save should be enabled
    /// </summary>
    /// <param name="Doc"></param>
    void Application_NewDocument(Microsoft.Office.Interop.Word.Document Doc)
    {
        buttonText = "New Document Created";
        ribbon.InvalidateControl("btnTest");
    }

     public string get_LabelName(IRibbonControl control)
    {
        return buttonText;
    }
}