Accessing Ribbon Controls Programatically in an XML Ribbon

浪子不回头ぞ 提交于 2019-12-06 08:47:56

问题


For programming Office Add-ins using C# 4.0, Microsoft provides two different ways of creating and/or modifying the Ribbon interface: you can use the Ribbon Designer or define the Ribbon's layout in Ribbon XML.

If you create a ribbon using the Ribbon designer, the class generated in the code behind has visibility to all the controls you've placed on the ribbon. So if I've placed a RibbonDropDown called "dropdown1", I could use the following code to add an item to it:

RibbonDropDownItem item = Factory.CreateRibbonDropDownItem();
item.Label = submatrix.Name;
item.Tag = submatrix;
this.dropDown1.Items.Add(item);

However, if you create the same Ribbon using Ribbon XML, dropDown1 or Factory aren't found ("The name does not exist in the current context").

Is there a way to access the items added to a Ribbon XML-defined ribbon in code?


回答1:


Might be a little late, but hopefully this helps someone.

I was utterly confused about this same issue. Turns out, you can only access these controls as string ids, and the model is heavy on invalidation events. So for example, when you get a button click via onAction method, you only have the sender's id from the control object, however, in this event handler, you can invalidate the other controls and have their events called using

ribbon.InvalidateControl("MyCtl");

check out this MS Lab, it has everything you need to get up and running



来源:https://stackoverflow.com/questions/7287998/accessing-ribbon-controls-programatically-in-an-xml-ribbon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!