Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,


Please Guide me for the getting the WPF TreeviewItem on the RightClick event of Mouse
I get the TreeViewItem on TreeView1_SelectedItemChanged event but Not able to find the treeViewItm on Right click of mouse.

Thanks and regards
vilas
Posted
Updated 7-Nov-12 3:05am
v2

1 solution

Right clicking the mouse on a TreeView does not select a node therefore there is no "node" to select. You can get the currently selected node within the event handler by casting the 'sender' parameter to a TreeView object then using the SelectedItem property (which returns the node object that is selected) or the SelectedItemPath to walk the control hierarchy and get the selected node. But I'm guessing that isn't what you want.

Unfortunately, the TV control doesn't readily return the node that a user right-clicks on... the sender is the TreeView and I believe the e.OriginalSource property also returns the TreeView (going from memory here...) I used a workaround that relied on the Mouse.DirectlyOver function in System.Windows.Input to return the UIElement the mouse is over. Assuming the mouse is over a TreeViewItem when the user right-clicked, you should be able to type-cast the returned UI element to a TreeViewItem in a Try..Catch block. If successful, you know the user was over a TVI element and now you have a reference to it. If you end up in the catch after the type-cast, you know the mouse was over something else when right-clicked. Of course, if you have other UI elements over the treeview, like panels or busy indicators, they must be set to collapsed rather than just transparent. Otherwise you will get them because they are higher in the Z-Order.

HTH
 
Share this answer
 
Comments
Mogya 8-Nov-12 0:03am    
Ok Thanks ,
But how Can I show the contextMenu On mouse Rightclick event on treeViewItem
and how to show different form on node selection changed, Please guide for this.
Thanks and regards
vilas
Jason Gleim 9-Nov-12 10:57am    
You can manually create a right-click pop-up menu using the ContextMenu class. You'll have to declare the items in the menu, link their events, and then add them to a ContextMenu instance. Do this for each of the two menus you want to have. Then, when you type-cast the node as in the answer above, just call the show method on the appropriate menu in the appropriate try/catch block. If you can successfully cast to a TreeViewNode object, show the menu for that. If that fails and you end up in the catch block, show the other.

Note that if you implement this logic you will probably want to use the Mouse Right-click Down event rather than the up. And be sure to set the 'handled' property on the event args parameter to true so that further right-click processing isn't done by the treeview itself.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900