combobox - 功能区 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 控件获取选中项的索引?
提前致谢,
西蒙娜
解决方案
不幸的是,在功能区组合框(源)中获取选择的索引是不可能的
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
推荐阅读
- android - 标准和自定义 GATT 特性
- optimization - 如何在 Codelite 中显示经过了多少时间?
- c++ - 为什么将排序键插入 std::set 比插入随机键快得多?
- json - 从大型复杂嵌套 json 中过滤各个级别的键
- spring-boot - 网站和移动应用程序的身份验证
- python - 通过函数将 tkinter 变量添加到字典
- javascript - 如何在 react-table 中使用 api 链接而不是本地 json 文件?
- python - 将两个不同长度列表的元素相加成一个新列表
- django - django 错误,“精确查找的 QuerySet 值必须限制为使用切片的一个结果”
- groovy - Groovy 注释中的默认数组值