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
getVisible
attribute with callback for groups and/or buttons in the same RibbonXML. Use a toggleButton to set a class level field, then trigger the Invalidate event for the Ribbon which will cause the callbacks of the "get" attributes to run. – Cindy Meister