RibbonDropDownItem单击侦听器
本文关键字:侦听器 单击 RibbonDropDownItem | 更新日期: 2023-09-27 18:01:06
我正在开发一个powerpoint插件,并使用功能区设计器来完成此操作。我有一个库项目,其中包含RibbonDropDownItems。我找不到在那里添加点击侦听器的方法,因为RibbonDropDownItem接口没有像RibbonButton接口那样的"点击"事件。
那么,有没有办法从RibbonDropDownItem中捕捉点击事件?
编辑:为办公室实施插件2013-2016
您需要订阅RibbonDropDown
控件的SelectionChanged事件。当用户在功能区下拉控件上选择新项目时,它将被激发。注意,SelectionChanged
事件仅在所选项目发生更改时才会引发,而不是在以下情况下:
- 当用户选择已经选择的项目时
- 当用户单击按钮时
- 为代码中的
SelectedItem
或SelectedItemIndex
属性指定新值时
最后,您可以在MSDN的以下系列文章中阅读更多关于Fluent UI控件的内容:
- 为开发人员自定义2007 Office Fluent功能区(第1部分,共3部分(
- 为开发人员自定义2007 Office Fluent功能区(第2部分,共3部分(
- 为开发人员自定义2007 Office Fluent功能区(第3部分,共3部分(
我找到了一个不使用XML方法的解决方案。
RibbonGallery类(包含我的RibbonDropDownItems(提供单击事件,该事件"当用户单击此Ribbon画廊上的项目时发生"。
因此,您可以使用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");
}
请注意,您应该小心使用反射方法,因为项标签和项目事件处理程序名称之间存在"隐藏"依赖关系。
我有一种方法,我觉得它很简单,可以完成任务。若要进行准备,请在RibbonBar上添加RibbonGallery对象。此对象有两个集合,一个用于RibbonButtons,另一个用于"Items"。后者正是这个问题所要问的。为了做好准备,这个RibbonGallery得到了一个Click处理程序,我创建了一个专门用于这个RibbonCallery的处理程序。当一个项目(来自第二个项目集合(被选中时,它会触发此事件:
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 )
{ }
}