RibbonDropDownItem单击侦听器

时间:2016-08-07 05:38:20

标签: c# vsto powerpoint office-interop office-addins

我正在开发一个powerpoint插件并使用功能区设计器来完成此操作。我有一个包含 RibbonDropDownItems 的图库项目。由于RibbonDropDownItem Interface没有the RibbonButton Interface之类的“点击”事件,我无法找到在其中添加点击侦听器的方法。

那么,有没有办法从RibbonDropDownItem中捕获click事件?

修改: 实施2013-2016办公室的插件

3 个答案:

答案 0 :(得分:1)

您需要订阅RibbonDropDown控件的SelectionChanged事件。当用户在功能区下拉控件上选择新项时会触发它。请注意,SelectionChanged事件仅在所选项目更改时引发,而不是在以下情况下引发:

  • 当用户选择已经选择的项目时。
  • 当用户点击按钮时。
  • 为代码中的SelectedItemSelectedItemIndex属性分配新值时。

最后,您可以在MSDN的以下系列文章中阅读有关Fluent UI控件的更多信息:

答案 1 :(得分:0)

我没有采用XML方法就找到了解决方案。

RibbonGallery类(包含我的RibbonDropDownItems)提供单击事件,“当用户单击此RibbonGallery上的项目时发生”。

因此,您可以使用 RibbonGallery点击侦听器来识别其中一个项目已被点击,然后使用 RibbonGallery#SelectedItem 检索所选项目。这是一个例子:

private void myDropdownGallery_Click(object sender, RibbonControlEventArgs e)
{
    //'ribbonGalleryObject' is the object created in Ribbon.Designer.cs
    RibbonDropDownItem item = ribbonGalleryObject.SelectedItem;

    string itemLabel = item.Label;

    if (itemLabel == "myItem1") {
        System.Windows.Forms.MessageBox.Show("Item 1 says hello");
    }
    else if (itemLabel == "myItem2"){
        System.Windows.Forms.MessageBox.Show("Item 2 says hello");
    }
}

此外,您可以依靠反射来区分 RibbonDropDownItems事件处理程序,并使您的架构与当前架构类似。

private void gallery1_Click(object sender, RibbonControlEventArgs e)
{
    //'ribbonGalleryObject' is the object created in Ribbon.Designer.cs
    RibbonDropDownItem item = ribbonGalleryObject.SelectedItem;

    string itemLabel = item.Label;
    string methodName = itemLabel + "_Click";

    System.Reflection.MethodInfo methodInfo = this.GetType().GetMethod(methodName);
    methodInfo.Invoke(this, null);
}

//click event handler for item 1
public void myItem1_Click()
{
    System.Windows.Forms.MessageBox.Show("Item 1 says hello");
}

//click event handler for item 2
public void myItem2_Click()
{
    System.Windows.Forms.MessageBox.Show("Item 2 says hello");
}

请注意,您应该小心使用反射方法,因为项标签项目事件处理程序名称之间存在“隐藏”依赖关系。

答案 2 :(得分:0)

我有一种简单易行的方法,可以完成工作。准备时,在RibbonBar上添加一个RibbonGallery对象。该对象有两个集合,一个集合用于RibbonButtons,另一个集合用于“ Items”。后者就是这个问题要问的问题。为此,此RibbonGallery获取一个Click处理程序,我创建了一个专门针对此RibbonGallery的处理程序。从第二个Items集合中选择一个项目时,它将触发此事件:

private void Handler_ItemClick( object sender, RibbonControlEventArgs e )
{
    try
    {
        String tag = GALLERY_FOR_THIS_SET_OF_ITEMS.SelectedItem.Tag;
        if (tag != null)
        {
            // use the tag to identify which "Item" it is. 
            // You also get access to other fields in the Item object
            // such as the name/id/label of the Item.
        }
    }
    catch (Exception)
    { }
}

StackOverflow问题询问菜单项,这是一种简单的方法。

作为一个相关的补充,如果您选择了第一个集合,即RibbonButtons的集合,该怎么办? RibbonButtons更易于使用。您可以按如下方式使用通用处理程序:

private void RibbonButtonHandler_Click( object sender, RibbonControlEventArgs e )
{
    try 
    {
            String tag = null;
            RibbonButton b = (RibbonButton)sender;              
            // Now that you have the button, you know what they wanted.
            tag = b.Tag;
            if ( tag != null )
            {
            }
    }
    catch( Exception )
    { }
}
相关问题