5

I have a context menu that looks like this

 A
 |--1
 |--2
 |--3

I need to access the object that the context menu is called from, after selecting 1 2 or 3

meaning if this is a context menu of a textbox1 then I need to access that object, how do I do that?

Forgot to mention, this is a WPF application. so Im using the System.Windows.Controls and the ContextMenu is created programmatically

H.B.
  • 166,899
  • 29
  • 327
  • 400
Bg1987
  • 1,129
  • 1
  • 11
  • 25
  • A context menu doesn't require having a owner control. Especially not for a TextBox, it implements its own context menu. But if you use the right way to invoke it then SourceControl is an excellent way to find the source control back. Be sure to use the Show() method that takes a Control. – Hans Passant Jul 16 '11 at 22:00
  • found the answer from a similar question http://stackoverflow.com/questions/1884117/get-owner-of-context-menu-in-code viky's code works, but I had to cast it twice. (I guess looping it is possible for better flexibility) – Bg1987 Jul 16 '11 at 22:20

6 Answers6

10

You can walk up the tree and get the control from the ContextMenu.PlacementTarget, e.g.

private void MenuItem_Click(object sender, RoutedEventArgs e)
{
    var item = sender as MenuItem;
    while (item.Parent is MenuItem)
    {
        item = (MenuItem)item.Parent;
    }
    var menu = item.Parent as ContextMenu;
    if (menu != null)
    {
        var droidsYouAreLookingFor = menu.PlacementTarget as TextBox;
        //...
    }
}
H.B.
  • 166,899
  • 29
  • 327
  • 400
3

You can look at the SourceControl property of the ContextMenuStrip that owns the context menu item that was clicked.

For example, in the Click handler for the menu item:

private void aToolStripMenuItem_Click(object sender, EventArgs e)
{
    var control = ((sender as ToolStripMenuItem).Owner as ContextMenuStrip).SourceControl;
       ...
}

Of course if you only have one ContextMenuStrip on the form, you can just reference it directly

var control = myContextMenuStrip.SourceControl;
Igby Largeman
  • 16,495
  • 3
  • 60
  • 86
  • I used MenuItem and a ContextMenu class. is there a difference between using a ContextMenuStrips and just a ContextMenu and MenuItems (the context menu is generated dynamically) besides the SourceControl property? – Bg1987 Jul 16 '11 at 22:02
1

Slight tweak to HB's answer. HB deserves the credit. Helped me find a DataGrid.

private void MenuItem_Click(object sender, RoutedEventArgs e)
{
    MenuItem item = sender as MenuItem;
    ContextMenu cm = (ContextMenu)item.Parent; 
    Popup popup = (Popup)cm.Parent; 

    var finalGoal = popup.PlacementTarget as DataGrid; 
}
rr789
  • 619
  • 2
  • 8
  • 10
0

Ugly Solution

I am searching for a better way to do the same thing. For now, the code below works:

TextBlock tb = ((sender as MenuItem).Parent as ContextMenu).PlacementTarget as TextBlock;

Replace TextBlock with your control's type.

Tyler Pantuso
  • 835
  • 8
  • 16
0

use the

ContextMenu.SourceControl

that's the variable that calls the context menu. all you need to do is cast the control

user744918
  • 13
  • 3
0

found the answer from a similar question

Get owner of context menu in code viky's code works, but I had to cast it twice.

I guess looping the casting of the Parent is possible for better flexibility (more casts depends on how deep the clicked item is)

Community
  • 1
  • 1
Bg1987
  • 1,129
  • 1
  • 11
  • 25