简体   繁体   English

C#= MenuItem.Click handler - 获取上下文菜单所属的对象?

[英]C# = MenuItem.Click handler - Get the object the context menu belongs to?

This might be incredibly simple and I'm not seeing it because it's the end of a long day, and if it is I apologize in advance. 这可能非常简单,我没有看到它,因为这是漫长的一天的结束,如果是我提前道歉。

I've got a set of Buttons that when right-clicked pop up a ContextMenu. 我有一组按钮,右键单击时会弹出一个ContextMenu。 The menu has two MenuItems, both of which have a Click handler function assigned. 该菜单有两个MenuItem,它们都分配了一个Click处理函数。 I'm triggering the ContextMenu to pop up on the right click of a button like so: 我正在触发ContextMenu弹出右键单击按钮,如下所示:

Overly simplified example: 过于简化的例子:


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; 

I need to be able to get the Button that triggered the ContextMenu to pop up IN the Click handler for the MenuItem, or get it to it somehow. 我需要能够获得触发ContextMenu的Button在MenuItem的Click处理程序中弹出,或以某种方式获取它。 MenuItem.Click takes EventArgs, so nothing useful there. MenuItem.Click接受EventArgs,所以没有什么用处。 I can cast object sender back to MenuItem but I can't find anything that tells me what made it pop up. 我可以将对象发送者强制转换回MenuItem,但我找不到任何告诉我是什么让它弹出的东西。 Is this possible? 这可能吗?

使用ContextMenu.SourceControl属性,它为您提供对Button实例的引用。

In the above code excerpt, when you call the show method of buttonContextMenu , you pass the button object to buttonContextMenu when you right-click on the button. 在上面的代码摘录中,当您调用buttonContextMenu的show方法时,右键单击按钮时将按钮对象传递给buttonContextMenu

To access the button in the OnFooClicked method simply cast the 'sender' back to a button. 要访问OnFooClicked方法中的按钮,只需将“发件人”转换回按钮即可。

public void OnFooClicked(object sender, EventArgs e)
{
     ((MenuItem)sender).Parent //This is the button
}

*I don't know if MenuItem is the correct cast but it should be along those lines. *我不知道MenuItem是否是正确的演员,但它应该沿着这些线。

If you are using ContextMenuStrip and ToolStripItem, rather than ContextMenu and MenuItem, you'll need: 如果您使用的是ContextMenuStrip和ToolStripItem,而不是ContextMenu和MenuItem,则需要:

private void myToolStripMenuItem_Click(object sender, EventArgs e)
{
   Button parent = (Button)((ContextMenuStrip)((ToolStripItem)sender).Owner).SourceControl;
   ...

With a regular ContextMenu and MenuItem (from trycatch's comment on Hans Passant's post): 使用常规的ContextMenu和MenuItem(来自trycatch对Hans Passant帖子的评论):

Button parent = (Button)((ContextMenu)((MenuItem)sender).Parent).SourceControl; 
button mybutton = ((ContextMenu)((MenuItem)sender).Parent).SourceControl as button;


Button buttonThatTriggeredTheContextMenu; 

public void OnButtonMouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
        // left click stuff handling
    if (e.Button == MouseButtons.Right) {
        buttonThatTriggeredTheContextMenu = (Button)sender;
        buttonContextMenu.Show((Button)sender, new Point(e.X, e.Y));
    }
}


public void OnFooClicked(object sender, EventArgs e)
{
    //buttonThatTriggeredTheContextMenu contains the button you want
}

I'm not 100% on this - its from code I wrote about 2 years ago, but I hope it gets you moving on this. 我不是百分之百 - 这是我2年前编写的代码,但我希望它能让你继续前进。

MenuItem item = (MenuItem)sender;
DependencyObject dep = item.Parent;
while((dep != null) && !(dep is Button))
    dep =  VisualTreeHelper.GetParent(dep);

if (dep == null)
    return;
Button button = dep as Button;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM