0

When I left click on a TreeView, it automatically selects the item under mouse. How can I do this for right click so when you right click, it also selects the item under mouse?

I want to do this because when I right click, I set the context menu of the TreeView when the SelectedItem changes, based on the e.NewValue of TreeView_SelectedItemChanged. But the right click itself doesn't change the SelectedItem, that's why the wrong menu shows up. Or I have to first left click to the item I want selected, and then right click.

Any ideas on how to do this?

Joan Venge
  • 315,713
  • 212
  • 479
  • 689
  • possible duplicate of [Select TreeView Node on right click before displaying ContextMenu](http://stackoverflow.com/questions/592373/select-treeview-node-on-right-click-before-displaying-contextmenu) – slugster Apr 01 '12 at 12:16
  • Another duplicate: [In WPF, how do I select the treeview item under my cursor on right-click?](http://stackoverflow.com/questions/797966/in-wpf-how-do-i-select-the-treeview-item-under-my-cursor-on-right-click) – slugster Apr 01 '12 at 12:17

2 Answers2

1

Override the right click event. Here is an example to do it.

pickypg
  • 22,034
  • 5
  • 72
  • 84
  • Thanks, you mean the mousedown event of the rightclick event? In the link it also shows the mousedown event. – Joan Venge Apr 21 '11 at 17:31
  • Correct. MouseDown is how you capture the user's Right-click. – pickypg Apr 21 '11 at 17:33
  • Thanks, but after that how can I perform a hittest to find the node that's clicked on? – Joan Venge Apr 21 '11 at 17:34
  • The MouseDown event is the same for WPF and WinForms. It is a bit unfortunate that WPF removed the "GetNodeAt" function. I believe you have two options. I believe that the `sender` is actually the TreeViewItem` that you want (`sender as TreeViewItem`; check for `null`) if they clicked on one, but you can also use the [VisualTreeHelper](http://msdn.microsoft.com/en-us/library/ms608752.aspx), which I have never used. – pickypg Apr 21 '11 at 17:49
  • Thanks I found an example of VisualTreeHelper which I am using now, so I can find the node. The last bit is to set this node as selected programmatically. Do you know how to do this? This is currently what I am looking into. – Joan Venge Apr 21 '11 at 17:51
  • Having never used it, I imagine you are looking to extract the TreeViewItem. Try `HitTestResult result = ...; TreeViewItem item = result != null ? result.VisualHit as TreeViewItem : null;` and check for `null`. – pickypg Apr 21 '11 at 18:01
1

The accpeted answer is correct about using the mouse down event, but are you sure you need Visual Tree Helper and Hit Testing? You might try something like:

var parent = this.DataContext as Parent;
var clicked = (sender as FrameworkElement).DataContext as Child;
parent.SelectedChild = clicked;

Remember, you can usually get to your viewmodel objects as the DataContext of a FrameworkElement. Using the Visual Tree and Hit Testing doesn't come up often.

default.kramer
  • 5,943
  • 2
  • 32
  • 50
  • 1
    Thanks, actually there are a few problems. One is I need to get the item under mouse when right clicked and so far I couldn't find a way. The other problem is selecting this item so it looks like you left clicked on it, then show my context menu. – Joan Venge Apr 21 '11 at 20:48