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

发布于 2024-10-31 19:37:20 字数 1073 浏览 0 评论 0原文

这可能非常简单,但我没有看到它,因为这是漫长的一天的结束,如果是的话,我提前道歉。

我有一组按钮,右键单击时会弹出一个上下文菜单。该菜单有两个 MenuItem,它们都分配有 Click 处理函数。我在右键单击按钮时触发 ContextMenu 弹出,如下所示:

过度简化的示例:


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; 

我需要能够在 Click 处理程序中获取触发 ContextMenu 弹出的按钮对于 MenuItem,或者以某种方式获取它。 MenuItem.Click 采用 EventArgs,因此没有任何用处。我可以将对象发送者强制转换回 MenuItem,但我找不到任何可以告诉我是什么让它弹出的信息。这可能吗?

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. The menu has two MenuItems, both of which have a Click handler function assigned. I'm triggering the ContextMenu to pop up on the right click of a button like so:

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. MenuItem.Click takes EventArgs, so nothing useful there. I can cast object sender back to MenuItem but I can't find anything that tells me what made it pop up. Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

赤濁 2024-11-07 19:37:20

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

Use the ContextMenu.SourceControl property, it gives you a reference to the Button instance back.

荒芜了季节 2024-11-07 19:37:20

在上面的代码摘录中,当您调用 buttonContextMenu 的 show 方法时,当您右键单击按钮时,将按钮对象传递给 buttonContextMenu

要访问 OnFooClicked 方法中的按钮,只需将“发送者”转换回按钮即可。

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

*我不知道 MenuItem 是否是正确的演员表,但它应该遵循这些原则。

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.

To access the button in the OnFooClicked method simply cast the 'sender' back to a button.

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.

雪若未夕 2024-11-07 19:37:20

如果您使用 ContextMenuStrip 和 ToolStripItem,而不是 ContextMenu 和 MenuItem,您将需要:

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

使用常规 ContextMenu 和 MenuItem(来自 trycatch 对 Hans Passant 帖子的评论):

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

If you are using ContextMenuStrip and ToolStripItem, rather than ContextMenu and MenuItem, you'll need:

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):

Button parent = (Button)((ContextMenu)((MenuItem)sender).Parent).SourceControl; 
静谧 2024-11-07 19:37:20


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
}



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
}

棒棒糖 2024-11-07 19:37:20

我并不是 100% 同意这一点——它来自我大约 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;

I'm not 100% on this - its from code I wrote about 2 years ago, but I hope it gets you moving on this.

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;
我不咬妳我踢妳 2024-11-07 19:37:20
button mybutton = ((ContextMenu)((MenuItem)sender).Parent).SourceControl as button;
button mybutton = ((ContextMenu)((MenuItem)sender).Parent).SourceControl as button;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文