# Selecting node on mouse right click

7 posts, 1 answers
1. ##### Ruben Hakopian
37 posts
Member since:
Nov 2009

Posted 23 Aug 2010 Link to this post

Hi,

I have a TreeView with a ContextMenu associated with it. When the context menu pops pup I need to know what is the item under the mouse pointer. However, when I check the "SelectedItem" property, it is not the item which is under the mouse pointer. I'm thinking about a way to make the item selected when right-click is used. Currently it is being selected only when a left-click is performed.

Thank you,
Ruben
##### Andi
39 posts
Member since:
Aug 2010

Posted 24 Aug 2010 Link to this post

Hi Ruben,

you can use the MouseRightButtonDown-Event from the TreeView to select the clicked node.

private void treeView1_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
    {
      RadTreeViewItem ClickedTreeViewItem = new RadTreeViewItem();

      //find the original object that raised the event
      UIElement ClickedItem = VisualTreeHelper.GetParent(e.OriginalSource as UIElement) as UIElement;

      //find the clicked TreeViewItem
      while ((ClickedItem != null) && !(ClickedItem is RadTreeViewItem))
      {
        ClickedItem = VisualTreeHelper.GetParent(ClickedItem) as UIElement;
      }

      ClickedTreeViewItem = ClickedItem as RadTreeViewItem;
      ClickedTreeViewItem.IsSelected = true;
      ClickedTreeViewItem.Focus();
    }
3. ##### Andi
39 posts
Member since:
Aug 2010

Posted 24 Aug 2010 Link to this post

I have found one small problem.
When you Rightclicked a node, the Context Menu opened correctly and the node is selected.
But when you now right-click an other node the Context Menu opens, but the node is not selected. Stepping through the code you will see, that the MouseRightButtonDown-Event from the TreeView is not fired.

One solution I have found is to use the Closed-Event from the Context Menu with this Code:
private void RadContextMenu_Closed(object sender, RoutedEventArgs e)
    {
      RadTreeViewItem item = (sender as RadContextMenu).GetClickedElement<RadTreeViewItem>();

      if (item != null)
      {
        item.IsSelected = true;
        item.Focus();
      }
    }
4. ##### Ruben Hakopian
37 posts
Member since:
Nov 2009

Posted 24 Aug 2010 Link to this post

Hi Andi,

Thank you very much! It works perfectly.

Cheers,
Ruben
5. ##### Anders
80 posts
Member since:
Dec 2010

Posted 06 Apr 2011 Link to this post

A good old-fashioned "me too!" - thanks for posting your solution, Andi!
6. ##### Sasireka
17 posts
Member since:
Nov 2012

Posted 29 Nov 2012 Link to this post

Hi,

I got the selected item through MouseRightButtonDown-Event
but how to focus the selected item. I am using RadGridview

Thanks,
Sasi
7. ##### Pavel R. Pavlov
1230 posts

Posted 04 Dec 2012 Link to this post

Hi Sasi,

The logic for selecting and focusing a GridViewRow is the same as selecting and focusing a RadTreeViewItem. The control exposes a method Focus() and a property IsSelected. Also you can use the following snippet to focus the third row (index 2) of the RadGridView control:

this.myGridView.Focus();
someGridViewRow= this.myGridView.GetRowForItem(this.myGridView.Items[2]);
someGridViewRow.IsSelected = true;
someGridViewRow.IsCurrent = true;
someGridViewRow.Focus();

Keep in mind that the current row\item always keeps the focus. And you can use the IsSynchronizedWithCurrentItem property to synchronize the Selected item with the Current item.

Regards,
Pavel R. Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.