Question

I am fairly new to Word AddIns and trying to add Ribbon Group(s). for each RibbonGroup i dynamically add RibbonMenu and then add RibbonButtons to menu inside a Group.

I got the groud work and it seems to be okie but whenever i access any collection

eg: group1.Items.add(new RibbonButton()); it throws me saying that Collection is ReadOnly.

But When i debugged the Collection's IsReadOnly flag is set to False which clearly tells that Collection is not readonly?

What am i missing.

Was it helpful?

Solution

The Collection gets read-only once your Ribbon gets loaded.

To modify the Ribbon control dynamically, you need to do it in the Ribbon constructor.

See http://msdn.microsoft.com/en-us/library/bb608623.aspx#SettingReadOnlyProperties

OTHER TIPS

Are you trying to add buttons to RibbonMenu or RibbonGroup? In your description you've said you want to add buttons to the menu, but the example you cited is trying to add a button to a ribbon group [I'm assuming group1 is a group not a menu!].

You should be able to add buttons to RibbonMenu, but if it's RibbonGroup you want then you're out of luck.
Here's the code comment for the Items property in RibbonGroup:

// Summary:
//     Gets the controls in the group.
//
// Returns:
//     The collection of controls in the group.
//
// Exceptions:
//   System.InvalidOperationException:
//     You try to add, remove, or replace a Microsoft.Office.Tools.Ribbon.RibbonControl
//     after the Ribbon is loaded.
IList<RibbonControl> Items { get; }

That's the reason you're getting an error, and that's also why the IsReadOnly property of the collection is set to false.
The Designer needs to be able to modify the items in RibbonGroup when it's creating the ribbon, but once it's loaded you can't make any modifications to it.

Note: If you want to add buttons to RibbonMenu on the fly, set Dynamic to True in your RibbonMenu item properties window (or you could put it in the Designer code), otherwise you'll run into the same exception again.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top