Adding Items to Ribbon Group at Runtime

Apr 10, 2009 at 8:11 PM
I need to be able to add new RibbonCommands and other items such as RibbonButtons to the Ribbon when my primary view opens or changes. I've tried adding them to an existing group but they do not show up, however, if I create a new group and add it, they show up correctly. My current workaround is terrible, I basically create a new group and add all the child controls from the original to the new one, remove the existing one and add mine, there has to be something I'm missing. Here's what I want to do:

foreach (RibbonCommand command in ribbonCommands) // ribbonCommands = new items to add
{
    shell.MainRibbon.Resources.Add(command.Name, command);
    group.Controls.Add(new RibbonButton { Command = command }); // group = RibbonGroup i want to add to
}

Jun 6, 2009 at 10:11 AM

Absolutely - I second this!!! Having this exact problem at the moment!

Cheers

Jun 6, 2009 at 10:28 AM

The Office UI guidelines and license state that you can't dynamically add and remove controls from the ribbon at runtime. However I suspect the reason this isn't working is because of the resizing code in the ribbon. You might need to find a way to trigger the group to re-evaluate its size, or to add the new controls to a specific size group.