So I have a dropdown menu in a ribbon with contents that can be changed while i开发者_如何学Ct is being used. Outlook is also happy to let me 'add' or 'insert' items into it, as long as I do not add more than 1 item.
If I try to, I'll be told that the index is out of bounds rather than expanding the upper bounds for me.
I find that if I insert it into the collection in the designer portion of the code, it will work fine, but designer code is only run once, unless I Dispose the ribbon and re-create it.
Any ideas regarding how I can get this working
Try this. This should work for you.
RibbonDropDownItem item
= Globals.Factory.GetRibbonFactory().CreateRibbonDropDownItem();
item.Label = "First Name";
this.cbRecent.Items.Add(item);
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.
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.
精彩评论