Dynamically change tape label

I created the VSTO ribbon for the Outlook 2010 add-in. When I used the designer before, I was able to dynamically change the label of the ribbon button. Now I am encoding this manually (XML / C #) and cannot determine how to accomplish the same thing. The label element in XML seems static.

FYI - the purpose of this is to identify the number of items in the gallery for the user.

Thanks.

+4
source share
2 answers

There is a getLabel attribute that you can set for your element. The value is the name of the callback function that is called to dynamically name the label. You can programmatically update the user interface to force all your callbacks to be called.

+4
source

This should work for Excel and Outlook using C #.

Based on your question, I assume that you can create most of the XML to get the button on the ribbon, so I will skip this part. You also did not mention which event the label button should change, so I assume that clicking this button will change the label text.

First set the XML button code in place

<button id="YourUniqueId" onAction="YourUniqueId_Click" getLabel="GetYourLabelText"> 

Create a class level variable that will be available to all methods in your tape class before the constructor, and then set the value in the constructor. You will also need a ribbon user interface class variable. This variable should be created when the tape is loaded using the load method in xml, such as onLoad = "Ribbon_Load".

 public class Ribbon: Office.IRibbonExtensibility { private bool _buttonClicked; private Office.RibbonUI ribbon; public Ribbon() { _buttonClicked = false; } public void Ribbon_Load(Office.IRibbonUI ribbonUI) { ribbon = ribbonUI; } } 

Further in your ribbon class, you will need two classes, YourUniqueId_Click and GetYourLabelText.

 public void YourUniqueId_Click(Office.IRibbonControl Control) { //Since the initial value is false and presumably the user just clicked for //the first (or N-th) time you'll want to set the value to true if(!_buttonClicked) { _buttonClicked = true; } //Or if clicking for a second (or N-th + 1) time, set the value to false else { _buttonClicked = false; } //Now use the invalidate method from the ribbon variable (from the load method) //to reset the specific control id (in this case "YourUniqueId") from the xml. //Invalidating the control will call the method "GetYourLabelText" ribbon.InvalidateControl(Control.Id); } public string GetYourLabelText(Office.IRibbonUI Control) { if(_buttonClicked) { return "Button is On"; } else { return "Button is Off"; } } 

The GetYourLabelText method is launched when the feed is initially loaded into outlook or excel. Since the class variable "_buttonClicked" is set to false in the constructor, the button label will begin as "Button off." Each time a button is clicked, "_buttonClicked" changes the logical state, and then the button is reset, again calling the "GetYourLabelText" method.

0
source

Source: https://habr.com/ru/post/1369029/


All Articles