这可能非常简单,我没有看到它,因为这是漫长一天的结束,如果是我提前道歉。C#= MenuItem.Click处理程序 - 获取上下文菜单所属的对象?
我有一组按钮,当右键单击时弹出一个ContextMenu。该菜单有两个MenuItem,它们都有一个Click处理函数分配。我触发文本菜单弹出按钮上的右键像这样:
过于简化的例子:
public void InitiailizeButtonContextMenu()
{
buttonContextMenu = new ContextMenu();
MenuItem foo = new MenuItem("foo");
foo.Click += OnFooClicked;
MenuItemCollection collection = new MenuItemCollection(buttonContextMenu);
collection.Add(foo);
}
public void OnButtonMouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
// left click stuff handling
if (e.Button == MouseButtons.Right)
buttonContextMenu.Show((Button)sender, new Point(e.X, e.Y));
}
public void OnFooClicked(object sender, EventArgs e)
{
// Need to get the Button the ContextMenu .Show'd on in
// OnButtonMouseClick... thoughts?
}
ContextMenu buttonContextMenu;
我需要能够得到触发文本菜单的按钮在MenuItem的Click处理程序中弹出,或以某种方式获取它。 MenuItem.Click采用EventArgs,因此在那里没有用。我可以将对象发送者重新映射回MenuItem,但是我找不到任何能告诉我是什么让它弹出的东西。这可能吗?
这是WinForms或WPF的问题吗? – 2011-04-12 00:03:11