Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the control of a context menu

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

like image 509
Bg1987 Avatar asked Dec 13 '22 11:12

Bg1987


1 Answers

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;
        //...
    }
}
like image 93
H.B. Avatar answered Dec 29 '22 04:12

H.B.