c# - CommandParameters in ContextMenu in WPF -


I have a scenario where I have a WPF TreeView control that has HierarchicalDataTemplate code> HierarchicalDataTemplate now, I have Label and with Label in ContextMenu for a menu Remove . The deleted menu item is forced to command called DeleteCommand which is a part of the class set as DataType as the HierarchicalDataTemplate has gone.

Now, I want to pass TreeView in Delete menuitem DeleteCommand CommandParameters in ContextMenu so that I can handle selection of TreeViewItems on currently removing selected items.

But if I bind the command parameter as {binding element name = TreeViewName} or whatever it may be for that matter, it always Unless the binded element is a property in DataContext .

Can anyone help me with the solution? I think, I have tried all possible things like Relative Soros and Esterrooyer type, but it is always null. For me, it looks like a bug in either a border or outline.

The problem is that ContextMenu is in the root of its own view tree, hence any RelativeSource.FindAncestor binding past ContextMenu will not go.

To use a solution property, a two step is to set binding to your label:

   & Lt; / ContextMenu & gt; & Lt; /Label.ContextMenu> & Lt; / Labels & gt;  

It's quite a hack, though. You are better off setting the property of your menu item in the placement target of the context menu and placing the command handle on your tree weigh. This means that you do not have to pass around the preview.


Comments