xml - 如何在 Excel 自定义功能区控件的下拉控件中设置默认值

标签 xml excel vba

我为 Excel 2010 创建了一个自定义 Fluent Ribbon 界面,其中包括一个下拉菜单。相关XML代码(简体):

<dropDown id="chooseFilter" showLabel="true" label="Filter" onAction="filterSelected" > 
    <item id="Filter1" label="Filter 1" /> 
    <item id="Filter2" label="Filter 2" /> 
</dropDown>

加载功能区时,未选择任何值 - 下拉列表看起来是空的。

enter image description here

我希望默认选择第一项 - 但找不到任何描述如何操作的文档。我看着 MSDN documentation对于控件,但它没有涵盖这种情况。我尝试了各种“类 HTML”语句的排列组合,但它们都被自定义 UI 编辑器视为无效而拒绝了。我尝试过的事情的例子:

<item id="Filter1" label="Filter 1" selected="selected" /> 

错误信息:The 'selected' attribute is not declared

我尝试了其他属性,例如 selectedItem , value , 和 selected<dropDown .../>声明,但似乎没有任何效果。

如果我有正确的文档,这将是微不足道的,但即使是完整的 Microsoft 功能区自定义“文档”(找到 here 也对这个主题保持沉默。

我什至试图查看架构是否位于 http://schemas.microsoft.com/office/2006/01/customui可能是“人类可读的”,但是当我试图在浏览器中打开它时,我被告知它不可用。也许有一个技巧......

所以我求助于这个论坛的综合智慧。从我的 Q/A 比率可以看出,我并不经常这样做......

如何修改我的 XML,以便打开功能区时在下拉控件中选择任意项目?我会接受它作为第一个项目 - 但“我选择在我的 XML 中声明的任何项目”会更好。

我正在为此寻找 XML 解决方案 - 宁愿不必添加 onLoad VBA 代码或其他 VBA 技巧。这有多难,对吧?...

最佳答案

看起来您需要使用 VBA 才能选择默认项。

引自documentation for the dropDown element (我的重点):

getSelectedItemID (getSelectedItemID callback)

Specifies the name of a callback function to be called to determine the identifier of the item to be selected in this control. The getSelectedItemID and getSelectedItemIndex attributes are mutually exclusive. If neither attribute is specified, the control SHOULD NOT display a selected item. For example, consider the following XML fragment:

<gallery id="gallery" getItemCount="GetGalleryItemCount"  
   getItemID="GetItemID"
   getSelectedItemID="GetGallerySelectedItemID" />

In this example, the GetGallerySelectedItemID callback function is called when the application needs to determine the selected item in the gallery. In this example the callback function returns one of the identifiers returned by the GetItemID callback function. The possible values for this attribute are defined by the ST_Delegate simple type, as specified in section 2.3.2.

根据我对文档的阅读,您应该自己维护过滤器的当前选定项。 GetSelectedItemID 处理程序将返回当前选定的项目,OnAction 处理程序将更新它。

在 XML 中:

<dropDown id="chooseFilter" showLabel="true" label="Filter"
   getSelectedItemID="GetSelectedItemID" onAction="OnAction"> 
   <item id="Filter1" label="Filter 1" /> 
   <item id="Filter2" label="Filter 2" />
</dropDown>

在您的工作簿的代码模块中:

Private mCurrentItemID As Variant

Sub GetSelectedItemID(control As IRibbonControl, ByRef itemID As Variant)
    If IsEmpty(mCurrentItemID) Then
        mCurrentItemID = "Filter1"
    End If
    itemID = mCurrentItemID
End Sub

Sub OnAction(control As IRibbonControl, selectedID As String, _
             selectedIndex As Integer)
    mCurrentItemID = selectedID
End Sub

关于xml - 如何在 Excel 自定义功能区控件的下拉控件中设置默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18906620/

相关文章:

java - 如何访问 Apache POI 上存储在 Sharepoint(带身份验证)上的 .xlsx 文件?

excel - Matlab 绘制一个 excel 单元格

android - 如何使用 AppCompat v21 在 android 中创建 float 操作按钮 (FAB)?

java - Android:简单的 XML SAX 解析器 - 反复显示相同的数据

excel - 当我读入一个变量时,为什么 matlab 返回这个变量?

vba - 自动分组Excel VBA

vba - 使用 VBA 宏将每个 Excel 工作表另存为单独的工作簿

vba - 在除命名工作表之外的所有工作表上运行删除重复代码

xml - XSLT:获取最新日期

javascript - 更改 json 提要响应 - 删除级别