How do I extend or derive from the RibbonToggleButton created by the Microsoft RibbonFactory?

42 views Asked by At

How do I extend/derive from RibbonToggleButton in this scenario?

I have a Microsoft Word VSTO add-in. My goal is to create a "custom" class MyRibbonToggleButton that extends or derives from Microsoft.Office.Tools.Ribbon.RibbonToggleButton. I want to extend/derive because there is at least one custom property by which I want to extend the RibbonToggleButton class.

My approach is to observe how a Microsoft.Office.Tools.Ribbon.RibbonToggleButton is created and then derive MyRibbonToggleButton class from what I see.

1. I created a new Word VSTO Project

enter image description here

enter image description here

2. Added a ribbon

enter image description here

3. Designed the ribbon, adding a RibbonToggleButton

enter image description here

When I examine the designer, I can see that a RibbonToggleButton object is instantiated by calling the ribbon's Factory.CreateRibbonToggleButton().

enter image description here

Since the type of the object returned is Microsoft.Office.Tools.Ribbon.RibbonToggleButton, I created a custom class and attempted to derive from RibbonToggleButton.

enter image description here

This is when I became aware that RibbonToggleButton is an interface. As such, my custom class has some 30 or so undefined properties, methods, events, etc.. I don't want to write the code behind all these. Rather, I want to utilize the properties, methods, and events provided in the base RibbonToggleButton class by Microsoft and I want to add at least one custom property.

Now I'm in trouble because I'm having a hard time understanding interfaces in general, not to mention how to extend/derive from one. I'm confused because I know there must be a concrete RibbonToggleButton object returned by Factory.CreateRibbonToggleButton() at run time. Again: I want to utilize the properties, methods, and events provided in the base RibbonToggleButton class by Microsoft and I want to add at least one custom property.

How do I extend/derive from RibbonToggleButton in this scenario?

1

There are 1 answers

0
Dmitry Streblechenko On

You cannot do that and I am not sure why you even need it. I'd also avoid ribbon designer if you can help it - just return the ribbon XML from your code.

Each ribbon control has an id (defined in you ribbon XML). If you need to keep your custom control in sync with a ribbon control, refresh the ribbon (by calling IRibbonUI.Invalidate or IRibbonUI.InvalidateControl. That will cause the ribbon to fire custom callbacks on your control (you need to specify them in ribbon XML), such as getImage, getText, etc. You can then return the updated information as appropriate.