Adding items to RibbonDropDown at runtime

两盒软妹~` 提交于 2019-12-04 23:46:53

Generally speaking, VSTO wants you to completely describe the UI elements you need one time, the very first time you're asked for them (via GetCustomUI).

I've run into similar probs before with vsto and about the only reasonable way around it I've found was to prepopulate (via the designer) all the elements you might need (so let's say 10 items in your drop down list).

Then, programmatically HIDE or SHOW those items and update their captions and other properties as necessary while your addin runs.

That way, you never have to dynamically add or remove anything.

Try this. This should work for you.

RibbonDropDownItem item 
      = Globals.Factory.GetRibbonFactory().CreateRibbonDropDownItem();
item.Label = "First Name";
this.cbRecent.Items.Add(item);
Lee

Try the following directly inside the Ribbon Class:

RibbonDropDownItem item = this.Factory.CreateRibbonDropDownItem();
item.Label = "Text";
combo.Items.Add(item);

jeds, your approach doesn't work with "new". You have to use the "Globals.Factory.GetRibbonFactory().CreateRibbonDropDownItem()". Otherwise, you are right and your approach works great with a RibbonGallery.

That approach also works great with a DropDown. I'm still often conflicted about which one to use...

However, other than those 2 objects (Dropdown and RibbonGallery), I believe drventure is correct. You simply have to stub out the objects ahead of time and use them as needed.

You can also use the XML Ribbon, but that creates an even bigger set of headaches (at least for my use cases).

Try using a Ribbon Gallery. I have been able to modify them during run-time with as little as

foreach (string s in list)
{
     RibbonDropDownItem item = new RibbonDropDownItem();                
     item.Label = s;
     rGallery.Items.Add(item);                
}

where rGallery is a RibbonGallery.

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