Home > DeveloperSection > Forums > Get owner of context menu on Button
Jayden Bell
Jayden Bell

Total Post:105

Points:739
Posted on    August-16-2013 6:24 AM

 WPF WPF 
Ratings:


 1 Reply(s)
 743  View(s)
Rate this:
Hi Developers!

Title just about says it all.I have a lot of buttons, all of them have the same context menu, I want to determine from the click event which button was rgiht clicked to get there.

This code does not work, placementTarget is null:

private void mi_Click(object sender, RoutedEventArgs e)

{

        Button contextMenuEzen = null;

        MenuItem mnu = sender as MenuItem;

        if (mnu != null)

        {

            ContextMenu ize =(ContextMenu)mnu.Parent;

            contextMenuEzen = ize.PlacementTarget as Button;

        }

}

Please help me!




shreesh chandra shukla
shreesh chandra shukla

Total Post:105

Points:735
Posted on    August-17-2013 6:49 AM

Hi!

RoutedEvents don't work exactly like regular events - the signature of the handler is EventHandler(object sender, RoutedEventArgs e). The RoutedEventArgs has a property called OriginalSource that represents the element in your UI that was actually clicked. In contrast, the sender parameter will always be the object on which the event handler is registered.

Thanks


Don't want to miss updates? Please click the below button!

Follow MindStick