
Ruben Hakopian
Top achievements
Rank 1
Ruben Hakopian
asked on 23 Aug 2010, 07:27 PM
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
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
6 Answers, 1 is accepted
0
Accepted

Andi
Top achievements
Rank 2
answered on 24 Aug 2010, 09:44 AM
Hi Ruben,
you can use the MouseRightButtonDown-Event from the TreeView to select the clicked node.
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();
}
0

Andi
Top achievements
Rank 2
answered on 24 Aug 2010, 11:03 AM
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:
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();
}
}
0

Ruben Hakopian
Top achievements
Rank 1
answered on 24 Aug 2010, 05:44 PM
Hi Andi,
Thank you very much! It works perfectly.
Cheers,
Ruben
Thank you very much! It works perfectly.
Cheers,
Ruben
0

Anders
Top achievements
Rank 1
answered on 06 Apr 2011, 07:18 AM
A good old-fashioned "me too!" - thanks for posting your solution, Andi!
0

Sasireka
Top achievements
Rank 1
answered on 29 Nov 2012, 02:37 PM
Hi,
I got the selected item through MouseRightButtonDown-Event
but how to focus the selected item. I am using RadGridview
Thanks,
Sasi
I got the selected item through MouseRightButtonDown-Event
but how to focus the selected item. I am using RadGridview
Thanks,
Sasi
0
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:
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
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.