none
Reference and Populate a DropDown using Ribbon XML with VS 2010 RRS feed

  • Question

  • Hello,

    Using VS 2010, I am creating a Ribbon for Excel 2013. My understanding is I have to use the Ribbon XML, I cannot use the Ribbon Designer.

    It is a very basic question that I cannot figure out: how do I reference the DropDown so I can dynamically populate it? 

    Here's the XML: 

    <?xml version="1.0" encoding="UTF-8"?>
    <customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="Ribbon_Load">
      <ribbon>
        <tabs>
          <tab idMso= "TabAddIns">       
            <group id="ContentGroup" label="Content">
              <button id="textButton" label="Insert Text"
                   screentip="Text" onAction="OnTextButton"
                   supertip="Inserts text at the cursor location."/>
              <button id="tableButton" label="Insert Table"
                   screentip="Table" onAction="OnTableButton"
                   supertip="Inserts a table at the cursor location."/>
              <dropDown id="ddlPrograms" label="Select Program" />        
            </group>
          </tab>
        </tabs>
      </ribbon>
    </customUI>
    

    After lots of searching, I have come very close, but I cannot seem to figure it out. I can also create a dropDown in C# with this:

       RibbonDropDown dropDown = Globals.Factory.GetRibbonFactory().CreateRibbonDropDown(); 

    but I then cannot seem to add it to the Ribbon.

    Any help would be greatly appreciated.

    Thank you.

    ~Jenna


    Wednesday, October 1, 2014 2:18 PM

Answers

  • Hello Jenna,

    There is no need to refer to the drop down control.

    You just need to return the data requested in the callbacks. For example, the getItemCount callbacks requests the number of items. Then the getItemID and getItemLabel callbacks are invoked to get the actual data for these items.

    Try to re-read the articles I mentioned in the previous post there.

    • Marked as answer by Jenna_Fire Thursday, October 2, 2014 2:19 PM
    Thursday, October 2, 2014 7:29 AM

All replies

  • Hello Jenna,

    The Ribbon UI is a static thing from its birth. You cannot create controls in the code like you do that in a regular .Net application. Instead, you have to use the Ribbon XML markup and return it to the host application when it is requested. See the GetCustomUI  method of the IRibbonExtensibility interface. The Ribbon Designer generates the XML markup under the hood. But not all features of the Ribbon UI are available when using the designer.

    You can populate the dropDown control explicitly specifying all the content at design time:

     <dropDown id="ChooseDepartment" showLabel="true" 
         label="Choose Department"> 
          <item id="Dept1" label="Shipping" /> 
          <item id="Dept2" label="Accounting" /> 
          <item id="Dept3" label="Engineering" /> 
       </dropDown> 
    

    Or use the callbacks for populating it at runtime. You might not know which items to display in a Ribbon control until run time. Callbacks make it possible to supply the values based on current conditions. For example, you might want to display a list of document headings. The following example demonstrates how you might provide a list of current document headings:

    <dropDown id="HeadingsDropDown" 
      getItemCount="GetItemCount" 
      getItemID="GetItemID" 
      getItemLabel="GetItemLabel"/>
    
    Sub GetItemCount(control As IRibbonControl, ByRef count)
        Dim varItems As Variant
        varItems = ActiveDocument.GetCrossReferenceItems(wdRefTypeHeading)
        count = UBound(varItems)
    End Sub
    
    Sub GetItemLabel(control As IRibbonControl, index As Integer, _
        ByRef label)
        Dim varItems As Variant
        varItems = ActiveDocument.GetCrossReferenceItems(wdRefTypeHeading)
        label = varItems(index + 1)
    End Sub
    
    Sub GetItemID(control As IRibbonControl, index As Integer, ByRef ID)
        ID = "heading" & index
    End Sub
    
    

    You can read more about the Fluent UI (aka Ribbon UI) in the following series of articles:

    Customizing the 2007 Office Fluent Ribbon for Developers (Part 1 of 3)

    Customizing the 2007 Office Fluent Ribbon for Developers (Part 2 of 3)

    Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3)

    Wednesday, October 1, 2014 4:56 PM
  • Eugene,

    I really appreciate the response, but I think I am missing something obvious.

    I think I understand how the CallBacks work, and I can create one to populate the dropdown. Where I am lost is how to refer to the dropdown and actually add the items to it dynamically.

    I can do that easily if I was able to use the RibbonDesigner. Here I am at a loss.

    Let's say I have this structure:

     public void PopulateDDL(Office.IRibbonControl Control)
       {
     string one = "Item One";
    string two = "Item Two";
    string three = "Item Three";
    
    }


    and I want to add the three string to the dropdown. How would I insert these items to the Control?

    Thank you.

    Wednesday, October 1, 2014 7:33 PM
  • Hello Jenna,

    There is no need to refer to the drop down control.

    You just need to return the data requested in the callbacks. For example, the getItemCount callbacks requests the number of items. Then the getItemID and getItemLabel callbacks are invoked to get the actual data for these items.

    Try to re-read the articles I mentioned in the previous post there.

    • Marked as answer by Jenna_Fire Thursday, October 2, 2014 2:19 PM
    Thursday, October 2, 2014 7:29 AM
  • Eugene,

    I was able to use the information you sent and I put it together with what I found here:

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/14381f8a-1cf6-4e0e-8a07-4376821f1918/populating-dropdown-dynamically-on-ribbon-load-in-ribbon-xml?forum=vsto

    Thank you for taking the time to assist me. It was a big help.  

    ~Jenna

    Thursday, October 2, 2014 2:19 PM