如何从这个插件类设置一个丝带按钮

本文关键字:一个 按钮 设置 插件 | 更新日期: 2023-09-27 18:02:47

我有一个ribbon,里面有一个button。当用户选择新文档时,我想更改功能区按钮的文本。我想除了这一行我什么都做了:

    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
    }

如何从这个插件类设置一个丝带按钮

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

this.Button.Label = "text";

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

在丝带:

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

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

您需要调用ribbon回调事件"getlabel"来设置文本。在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;
    }
}