Skip to main content
 首页 » 编程设计

c#之VSTO : how to set a ribbon's button from ThisAddIn class

2024年02月13日16EasonJim

我有一条丝带,上面有纽扣。 当用户选择新文档时,我想更改功能区按钮的文本。 我想我什么都做了,除了一行:

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

在这个加载项中:

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