首页 > 解决方案 > 功能区 XML - 在组合框控件中获取选定的索引或项目 ID

问题描述

我正在开发一个comboBox XML Ribbon Control,我要疯了才能获得所选项目的索引。

这是带有组合框的功能区 XML 代码:

<customUI onLoad="Ribbon_Load" xmlns="http://schemas.microsoft.com/office/2009/07/customui">
<ribbon>
  <tabs>

    <tab id="SearchCustomerTab" insertAfterMso="TabAddIns" label="Cliente" visible="true">
      <group id="SearchCustomerGroup" label="Cliente" autoScale="true">
        <comboBox id="CustomerComboBox" getItemCount="GetItemCountCallback" getItemLabel="GetItemLabelCallback" getItemID="GetItemIDCallback" onChange="OnChangeCallback" />
      </group>
    </tab>
  </tabs>
 </ribbon>
</customUI>

使用 getItemCount 和 getItemLabel 回调,我正确填充了 che comboBox(oTabCustomersList 是自定义类的列表):

Public Function GetItemCountCallback(ByVal control As Office.IRibbonControl) As Integer
    Return oTabCustomersList.Count

End Function

Public Function GetItemLabelCallback(ByVal control As Office.IRibbonControl, index As Integer) As String
    Return oTabCustomersList(index).NomeCompleto

End Function

使用 getItemId 回调,我设置了 ID 中每个项目的索引:

Public Function GetItemIDCallback(ByVal control As Office.IRibbonControl, index As Integer) As String
    Return index.ToString

End Function

但通过 onChange 回调,我可以获得项目标签,但不能获得 ID 或所选索引:

Public Sub OnChangeCallback(ByVal control As Office.IRibbonControl, text As String)

    Debug.WriteLine("OnChangeCallback text: " & text) 'text = item label

End Sub

有没有办法用 Ribbon comboBox 控件获取选中项的索引?

提前致谢,

西蒙娜

标签: comboboxvstoms-officeribbonx

解决方案


不幸的是,在功能区组合框(源)中获取选择的索引是不可能的

Whenever the value of the combo box is selected, the onChange callback receives the text. 但是,无法获得选择的索引。

我已经使用 Dictionary (of String, CustomClass) 解决了,其中字符串是 OnChangeCallback 的文本参数:

Private customClass As CustomClass
Private customDictionary As Dictionary(Of String, CustomClass)

Public Sub Ribbon_Load(ByVal ribbonUI As Office.IRibbonUI)
    Dim customList As List(Of CustomClass)

    customList = FunctionToPopulateMyList()
    customDictionary = customList.ToDictionary(Function(p) p.MyText, Function(p) p)

End Sub

Public Function GetItemLabelCallback(ByVal control As Office.IRibbonControl, index As Integer) As String

        Return oCustomDictionary.ElementAt(index).Value.MyText
End Function

Public Function GetItemCountCallback(ByVal control As Office.IRibbonControl) As Integer

        Return oCustomDictionary.Count

End Function

Public Function GetItemIDCallback(ByVal control As Office.IRibbonControl, index As Integer) As String
    Return "Item" & index.ToString & control.Id

End Function

Public Sub OnChangeCallback(ByVal control As Office.IRibbonControl, text As String)
    If (customDictionary.ContainsKey(text)) Then
        customClass = customDictionary(text)

    End If
End Function

推荐阅读