Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF
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 7-Nov-12 3:04am
Mogya1.6K
Edited 7-Nov-12 3:05am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
Mogya at 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 at 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)

  Print Answers RSS
0 OriginalGriff 420
1 CHill60 410
2 Sergey Alexandrovich Kryukov 384
3 PIEBALDconsult 235
4 BillWoodruff 190


Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 7 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100