The VSTO Ribbon Designer "wraps" up Ribbon XML so that the developer can work with basic controls in a way similar to using Windows Forms controls. This means that properties (attributes in the XML) can be read and written to at run-time, rather than via callbacks. Also, the Ribbon control objects expose events rather than the developer needing to code event callbacks.
(Note that for anything more complex, it's necessary to use Ribbon XML. The Designer really only works for basic controls, added from the Toolbox.)
The documentation is here. Below is a small example to demonstrate.
For the example, a DropDown
control has been added to a Ribbon group, named DropDown1
. It can be found in the code file Ribbon1.Designer.cs
where it is declared as
internal Microsoft.Office.Tools.Ribbon.RibbonDropDown dropDown1;
and implemented with
// dropDown1
//
ribbonDropDownItem1.Label = "Item0";
ribbonDropDownItem2.Label = "Item1";
ribbonDropDownItem3.Label = "Item2";
ribbonDropDownItem4.Label = "Item3";
this.dropDown1.Items.Add(ribbonDropDownItem1);
this.dropDown1.Items.Add(ribbonDropDownItem2);
this.dropDown1.Items.Add(ribbonDropDownItem3);
this.dropDown1.Items.Add(ribbonDropDownItem4);
this.dropDown1.Label = "dropDown1";
this.dropDown1.Name = "dropDown1";
this.dropDown1.SelectionChanged += new System.EventHandler<Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs>(this.dropDown1_SelectionChanged);
Note that the information in this file is generated by the "Tools" and is not meant to be edited by the Developer. It can be, but there's no guarantee the "Tools" won't make changes negating any edits. But from it one can see what is executed when the Ribbon is loaded (attribute settings and loading the list) and the creation of an event (which would be a callback in Ribbon XML). All the properties and settings shown are generated using the Properties
window provided in the Designer, same as for Windows Form controls.
Then a button was added to a Ribbon group (any tab, any group) in the project. Double-click to open the code page in Ribbon1.cs
with the default Click
handler and the a code stub was added. The code between the braces is typed by the developer.
Note how this code uses properties, which are read (this.dropDown1.SelectedItem.Label
) and set (ddItem.Label
). The code shows a message with the caption of the currently selected item, creates a new item, setting the caption and a screen tip, adds it to the dropdown, then selects that item.
private void btnSetGetDD1_Click(object sender, RibbonControlEventArgs e)
{
MessageBox.Show(this.dropDown1.SelectedItem.Label);
RibbonDropDownItem ddItem = new RibbonDropDownItem();
ddItem.Label = "new item three added";
ddItem.ScreenTip = "next test";
this.dropDown1.Items.Add(ddItem);
this.dropDown1.SelectedItemIndex = this.dropDown1.Items.Count - 1;
}
Set
callbacks in Ribbon XML because there is no event to handle them - a callback needs a trigger. So the "setting" is part of the "get" (and other) callbacks. – Cindy Meisterribbon.xml
andribbon.vb
. Previously I said I tweak the XML, but I neglected to say I also tweak the generated VB file as well. That's where I create the subs and functions foronAction
,getPressed
, etc. I also added functionality to handle unrecognized control signatures as well, which has been very helpful. Does any of this sound like what you're referring to? I'm curious about what you describe of working with properties and events instead of callbacks. Could you elaborate on that, or point me to some reading so I could learn about it? – spinjector