Home‎ > ‎

Drop down button in Office Ribbon XML

posted Oct 11, 2012, 1:11 AM by Pieter-Bas IJdens   [ updated Oct 11, 2012, 1:13 AM ]
For a customer we were asked to create a split-button with a drop-down menu in an office ribbon. Documented here is how we did this:

Result:

Sample


The Ribbon XML:
<?xml version="1.0" encoding="utf-8" ?>
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="OnLoad">
  <ribbon>
    <tabs>
      <tab id="TabTEST" label="Test 123" visible="true">
        <group id="MyGroup" label="My Group" visible="true">
          <box id="Box0" boxStyle="horizontal">
            <labelControl id="Label0" label="Click or drop down to select: " />
            <splitButton id="SplitButton0" size="normal">
              <button id="Button0" label="Action" onAction="RibbonControlButtonAction" showImage="false" />
              <menu id="Menu0">
                <toggleButton id="Option1" label="Option one for action" onAction="RibbonControlToggleButtonAction" getPressed="GetIsPressed"/>
                <toggleButton id="Option2" label="Option two for action" onAction="RibbonControlToggleButtonAction" getPressed="GetIsPressed"/>
              </menu>
            </splitButton>
          </box>
        </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>
The code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Core;
using System.Reflection;
using System.IO;
using System.Runtime.InteropServices;
 
namespace WordAddInDropDownMenu
{
    [ComVisible(true)]
    public class Ribbon1 : IRibbonExtensibility
    {
        public string GetCustomUI(string ribbonID)
        {
            return GetResourceText("WordAddInDropDownMenu.Ribbon1.xml");
        }         public void OnLoad(IRibbonUI ribbon)         {             _ribbon = ribbon;         }         private static string GetResourceText(string resourceName)         {             Assembly asm = Assembly.GetExecutingAssembly();             using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceName)))             {                 return resourceReader.ReadToEnd();             }         }         public void RibbonControlButtonAction(IRibbonControl control)         {             if ("Button0" == control.Id)             {                 DoAction();             }         }         public void RibbonControlToggleButtonAction(IRibbonControl control, bool pressed)         {             _lastPressed = control.Id;             // Make sure all buttons are updated, could also search for specific buttons and invalidate those.             _ribbon.Invalidate();             DoAction();         }         private void DoAction()         {             System.Windows.Forms.MessageBox.Show("Selected " + _lastPressed, "Message");         }         private String _lastPressed = "Option1";         public bool GetIsPressed(IRibbonControl control)
        {
            return control.Id.Equals(_lastPressed);
        }
 
        private IRibbonUI _ribbon; 
    }
}
Attached all code an a VSTO Add-In.
ċ
WordAddInDropDownMenu.zip
(15k)
Pieter-Bas IJdens,
Oct 11, 2012, 1:11 AM
Comments