Update Controls using Ribbon XML RRS feed

  • Question

  • I would like to change the label of a menu control programmatically. So that when a user clicks on a button within the menu, the menu's label is updated. Could this be implemented with a callback method?

    Tom Houston
    Tuesday, January 17, 2012 5:58 PM


  • Hi Tom,

    Here's a C# Ribbon XML example of how to create dynamic button labels using a toggle button -- but it can be used for other controls as well.

    1. In your ribbon XML, create the toggle button and include the getLabel, onAction, and getPressed attributes:


    <toggleButton id="MyButton_Enable"  getLabel="MyButton_getLabel" onAction="MyButton_EnableDisable_Clicked" getPressed="MyButton_getPressed" />


    2. In your ribbon.cs file, create a boolean global whose state is used to determine the label; here, that's "_myButtonToggleState".   Then, wire up onAction, getLabel, and getPressed:


             private bool _myButtonToggleState= false;

    /// <summary> /// Handle the MyButton Enabled/Disabled click event /// </summary> /// <param name="control">Control that was clicked</param> /// <param name="state">new state of the button</param> public void MyButton_EnableDisable_Clicked(Office.IRibbonControl control, bool state) { _myButtonToggleState = state;

     // do stuff in response to the button click ... // force the button state to be re-evaluated _ribbon.InvalidateControl("MyButton_Enable"); }

    /// <summary>
    /// Function that returns the button label as "On" or "Off"
    /// </summary>
    /// <param name="control">MyButton Enable/Disable control</param>
    /// <returns>Updated label string</returns>
    public string MyButton_getLabel(Office.IRibbonControl control)
    // ... set _myButtonToggleState as appropriate ... then return

    return ((_myButtonToggleState== true) ? "On" : "Off");

    /// <summary>
    /// Set the Pressed state of MyButton
    /// </summary>
    /// <param name="control">MyButton Enable/Disable control</param>
    /// <returns>True if button should appear pressed</returns>
    public bool MyButton_getPressed(Office.IRibbonControl control)
    // The following line assumes that MyButton_getLabel has already been called
    return _datalogToggleState;


    For other use cases, you might have different external factors than a button click that change the state of the boolean (and thus, the label) whenever the button is invalidated.   Good luck and have fun!



    Tuesday, January 17, 2012 8:56 PM