GET OWNER OF CONTEXT MENU ON BUTTON

Jayden Bell

Total Post:110

Points:774
Posted by  Jayden Bell
WPF 
WPF
 1070  View(s)
Ratings:
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!


  1. shreesh chandra shukla

    Post:105

    Points:735
    Re: Get owner of context menu on Button

    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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!