I have a TreeView control with a bunch of TreeNodes. Each nodes ContextMenu has different MenuItems based on its state. So I am currently attaching each TreeNode its own ContextMenu.
TreeView tv = new TreeView();
TreeNode tn = New TreeNode();
tn.ContextMenu = GetContextMenu(state);
tv.Nodes.Add(tn);
Then in the click event for the MenuItem I try to get the TreeNode that the ContextMenu belonged to.
MenuItem mi = (MenuItem)sender;
ContextMenu tm = mi.GetContextMenu();
var sc = tm.SourceControl;
The problem is that tm.SourceControl == null
. I noticed that TreeNode
doesn't derive from Control
. Is that why the SourceControl
property is null? How can I get the appropriate TreeNode object? Or even the TreeView object?