I have found a solution without going to the XML approach.
The RibbonGallery class (which contains my RibbonDropDownItems) provides the click event which "Occurs when a user clicks an item on this RibbonGallery".
So you could use the RibbonGallery click listener to identify that one of the items was clicked and then retrieve the selected item with the RibbonGallery#SelectedItem. Here is an example:
private void myDropdownGallery_Click(object sender, RibbonControlEventArgs e)
{
//'ribbonGalleryObject' is the object created in Ribbon.Designer.cs
RibbonDropDownItem item = ribbonGalleryObject.SelectedItem;
string itemLabel = item.Label;
if (itemLabel == "myItem1") {
System.Windows.Forms.MessageBox.Show("Item 1 says hello");
}
else if (itemLabel == "myItem2"){
System.Windows.Forms.MessageBox.Show("Item 2 says hello");
}
}
Furthermore, you could rely on reflection to distinguish the RibbonDropDownItems event handlers and keep your architecture similar as the current one.
private void gallery1_Click(object sender, RibbonControlEventArgs e)
{
//'ribbonGalleryObject' is the object created in Ribbon.Designer.cs
RibbonDropDownItem item = ribbonGalleryObject.SelectedItem;
string itemLabel = item.Label;
string methodName = itemLabel + "_Click";
System.Reflection.MethodInfo methodInfo = this.GetType().GetMethod(methodName);
methodInfo.Invoke(this, null);
}
//click event handler for item 1
public void myItem1_Click()
{
System.Windows.Forms.MessageBox.Show("Item 1 says hello");
}
//click event handler for item 2
public void myItem2_Click()
{
System.Windows.Forms.MessageBox.Show("Item 2 says hello");
}
Note that you should be careful with the reflection approach because there is a "hidden" dependency between the item label and the item event handler name.