在哪个按钮上触发了菜单项弹出窗口 [英] ON which button was the menuitem popup triggered
本文介绍了在哪个按钮上触发了菜单项弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有菜单项的按钮.我需要知道"ON"是弹出按钮中的哪个被触发了吗?
我尝试过类似的方法:
I have a button set up with menu items. I need to know "ON" which of those buttons was the pop up triggered?
I tried something like :
ContextMenu ct = (ContextMenu)((MenuItem)sender).Parent;
但是除了作为菜单项的父项获得菜单"之外,我什么也做不了-.-
but I cant go anywhere further than get the "Menu" as a parent of MenuItem -.-
推荐答案
如果按钮触发了弹出菜单,则没有访问方式.您所能做的就是访问菜单.菜单项应具有标签属性.您的按钮可以在显示菜单之前进行设置,而这可能就是您所知道的.我认为它是一个对象,因此您可以将其设置为控件或字符串.
If a button triggered the popup menu, there''s no way to access that. All you can do is access the menu. The menu item should have a tag property. Your button could set that before showing the menu and that could be how you tell. I think it''s an object so you could set it to be the control, or a string.
不幸的是,上下文菜单不在可视树的范围内.以下是有关尝试在MenuItem中进行绑定的一些评论: http://stackoverflow. com/questions/1013558/elementname-binding-from-menuitem-in-contextmenu [
Unfortunately, the context menu is outside of the visual tree. Here is some comments on attempting to work with binding in a MenuItem: http://stackoverflow.com/questions/1013558/elementname-binding-from-menuitem-in-contextmenu[^]
这是一个解决方案...
Here is a solution...
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
MenuItem mnu = sender as MenuItem;
Button ct = null;
if (mnu != null)
{
ct = ((ContextMenu)mnu.Parent).PlacementTarget as Button;
MessageBox.Show("Clicked " + ct.Name);
}
}
这篇关于在哪个按钮上触发了菜单项弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文