0

I have a C# VSTO Excel add-in that uses XML for the ribbon. In it, there are multiple ToggleButtons that all use the same functions used in their 'getLabel', 'getKeytip', 'getScreentip', 'getSupertip', 'getPressed', & 'onAction' callbacks. Those functions then return the correct value or execute the correct code based on the control's ID.

Is it possible to create a 'template' for these elements that sets these attributes, but allows me to provide the ID?

For example here is what I currently have:

          <toggleButton
            id="tb1"
            getLabel="GetLabel"
            getKeytip="GetKeytip"
            getScreentip="GetScreentip"
            getSupertip="GetSupertip"
            getPressed="Togglebutton_GetPressed"
            onAction="Togglebutton_OnAction"/>
          <toggleButton
            id="tb2"
            getLabel="GetLabel"
            getKeytip="GetKeytip"
            getScreentip="GetScreentip"
            getSupertip="GetSupertip"
            getPressed="Togglebutton_GetPressed"
            onAction="Togglebutton_OnAction"/>
          <toggleButton
            id="tb3"
            getLabel="GetLabel"
            getKeytip="GetKeytip"
            getScreentip="GetScreentip"
            getSupertip="GetSupertip"
            getPressed="Togglebutton_GetPressed"
            onAction="Togglebutton_OnAction"/>
          <toggleButton
            id="tb4"
            getLabel="GetLabel"
            getKeytip="GetKeytip"
            getScreentip="GetScreentip"
            getSupertip="GetSupertip"
            getPressed="Togglebutton_GetPressed"
            onAction="Togglebutton_OnAction"/>
          <toggleButton
            id="tb5"
            getLabel="GetLabel"
            getKeytip="GetKeytip"
            getScreentip="GetScreentip"
            getSupertip="GetSupertip"
            getPressed="Togglebutton_GetPressed"
            onAction="Togglebutton_OnAction"/>

And I'd like to be able to specify a 'template':

          <toggleButtonTemplate
            getLabel="GetLabel"
            getKeytip="GetKeytip"
            getScreentip="GetScreentip"
            getSupertip="GetSupertip"
            getPressed="Togglebutton_GetPressed"
            onAction="Togglebutton_OnAction"/>

And then have my ribbon XML be updated to something like:

          <toggleButtonTemplate
            id="tb1"/>
          <toggleButtonTemplate
            id="tb2"/>
          <toggleButtonTemplate
            id="tb3"/>
          <toggleButtonTemplate
            id="tb4"/>
          <toggleButtonTemplate
            id="tb5"/>

Is something like this possible? If so, how would I go about doing it?

1 Answers1

0

Yes, it is possible. Use .net BCL for dealing with XML files. You can generate the ribbon XML at runtime and add attributes to the XML files dynamically using the XmlDocument class from the System.XML namespace. See Adding attributes to an XML node for more information.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Thank you for the suggestion and I think I get what you're saying, but how would I go about making it use the generated ribbon XML? – Kyle Beckman May 03 '22 at 00:42
  • @KyleBeckman search along these lines https://stackoverflow.com/a/44264070/495455 – Jeremy Thompson May 03 '22 at 02:24
  • I wanted to report back that I was able to figure it out (took me longer than it should TBH)... I realized that in the Ribbon.cs file that got added alongside the XML, there was the GetCusomUI function that returns the text of the XML. I added some code that "injects" the attributes as needed using the class referenced above. Thanks everybody for the help! – Kyle Beckman May 03 '22 at 19:12