Search code examples
visual-studio-2017vstooffice-interopribbonx

VSTO 4/XML: how to make certain controls optionally visible


I've a XML Ribbon made by VSTO-4 and VS2017 functioning in Outlook-2016. I'm not using the Designer provided by Visual Studio, but the entire "Fluent" mode (XML).

This Ribbon if fulfilled with buttons and my clients are "a little bit" lost with so many buttons/options in the same ribbon... and most of them are really just "options" of my program.

I would like to make this Ribbon (named now Ribbon-1) with just 3 buttons, one of them an "OPTIONS" command-button, to call the Ribbon-2, this one filled with all other buttons I have now in Ribbon-1.

Obviously, Ribbon-2 will appear at the same TAB of Ribbon-1 (as least "appearing be in the same TAB") and, once the user set an option, he click on "BACK" button and Ribbon-2 disappear and Ribbon-1 appears again...

We can see this behaviour in some AddIns and I would like to make the same.

Any suggestion? I appreciate any tip.


Solution

  • Rather than using multiple Ribbons it might make sense to put all the controls in one Ribbon. Use the getVisible attribute to set the visibility of all buttons and groups that should optionally be hidden or visible. Use a toggleButton to show/hide these buttons.

    The onAction callback for the toggleButton can set a class-level variable that the getVisible callbacks can check. The procedure then invalidates the Ribbon so that the getVisible callbacks are triggered. These, in turn, check the class-level variable to determine the visibility state of each button.

    Note that get callbacks are also executed when the Ribbon loads.

    Sample Ribbon XML:

    <group id="MyGroup" label="TEST empty" visible="true">
      <button id="testButton" label="test empty" visible="true"/>
      <toggleButton id="testToggle" label="toggle optional buttons" visible="true" onAction="toggleVisibleControls"/>
      <button id="optionalButton" label ="optional" getVisible="isVisible" />
    </group>
    <group id="Optional" label="Optional group" getVisible="isVisible"></group>
    

    Sample VB.NET code for a VSTO Ribbon XML:

    'Generated by VSTO
    <Runtime.InteropServices.ComVisible(True)> _
        Public Class Ribbon1
        Implements Office.IRibbonExtensibility
    
        Private ribbon As Office.IRibbonUI
        Private ShowHide As Boolean = False
    
        Public Sub New()
        End Sub
    
        Public Function GetCustomUI(ByVal ribbonID As String) As String Implements Office.IRibbonExtensibility.GetCustomUI
            Return GetResourceText("VB2010addin_RibbonXML.Ribbon1.xml")
        End Function
    
    #Region "Ribbon Callbacks"
        'Create callback methods here. For more information about adding callback methods, select the Ribbon XML item in Solution Explorer and then press F1.
        Public Sub Ribbon_Load(ByVal ribbonUI As Office.IRibbonUI)
            Me.ribbon = ribbonUI
         End Sub
    
        Public Function isVisible(ByVal control As Office.IRibbonControl) As Boolean
            Return Me.ShowHide
        End Function
    
        Public Sub toggleVisibleControls(ByVal control As Office.IRibbonControl, pressed As Boolean)
            ShowHide = pressed
            ribbon.Invalidate()
        End Sub
    
    #End Region