I've given my WPF ListView a context menu:
<TreeView ContextMenuOpening="TreeView_ContextMenuOpening">
<TreeView.ContextMenu>
<ContextMenu>
<MenuItem Name="NewInputMenuItem" Header="Add" Click="AddInputMenuItem_Click" />
<MenuItem Name="RemoveInputMenuItem" Header="Remove" Click="RemoveInputMenuItem_Click" />
</ContextMenu>
</TreeView.ContextMenu>
<!-- etc... -->
</TreeView>
I've defined the context menu on theTreeView
rather than the TreeViewItem
as I want the same context menu displayed regardless of whether or not an item is selected, however I do want the "Remove" menu item to be enabled only if the user has right clicked on an item, not just on empty space in the menu.
The way that I'm currently handling this is to use the selected item property of the TreeView
(in the TreeView_ContextMenuOpening
event handler), however the problem is that right clicking on a tree view item opens the context menu for that tree view without changing the selected state of the tree view item.
Also, I can't help but think that all of the above is very un WPF-like, so:
- How can I make it so that when the user right clicks on a tree view item, that item is selected.
- And is there a better way of achieving the above?