更新时间:2023-02-13 20:36:26
如果按钮触发了弹出菜单,则没有访问方式.您所能做的就是访问菜单.菜单项应具有标签属性.您的按钮可以在显示菜单之前进行设置,而这可能就是您所知道的.我认为它是一个对象,因此您可以将其设置为控件或字符串.
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://***. 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://***.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);
}
}