Click here to Skip to main content
15,894,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :
C#
ContextMenu ct = (ContextMenu)((MenuItem)sender).Parent;


but I cant go anywhere further than get the "Menu" as a parent of MenuItem -.-
Posted
Updated 24-Aug-12 3:27am
v5

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.
 
Share this answer
 
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[^]
 
Share this answer
 
Here is a solution...

C#
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);
            }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900