This is a migrated thread and some comments may be shown as answers.

Right Click Context Menu Problem

5 Answers 104 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
JDT
Top achievements
Rank 1
JDT asked on 20 May 2010, 06:42 AM
Hi Telerik, appreciate the help as always. Here's the problem. When I right click a node (it doesn't get selected, it should, but since it doesn't I can't use the selecteditem property of the tree). I therefore use the method in the documentation.

 RadMenuItem menuItem = ((RadRoutedEventArgs)e).OriginalSource as RadMenuItem; 
 RadTreeViewItem riParent = FindParentOfType<RadTreeViewItem>(menuItem); 


private static T FindParentOfType<T>(UIElement element) where T : UIElement 
        { 
            if (element == null) 
                return null; 
            DependencyObject parent = 
                Telerik.Windows.RoutedEvent.GetLogicalParent(element) ?? 
                VisualTreeHelper.GetParent(element); 
            while ((parent != null) && !(parent is T)) 
            { 
                parent = 
                    Telerik.Windows.RoutedEvent.GetLogicalParent(parent) ?? 
                    VisualTreeHelper.GetParent(parent); 
            } 
            return (T)parent; 
        } 

The FindParentOfType<T> method ALWAYS returns null. I can't get it to return the object that was right clicked. Any suggestions? I would prefer that the right click just selected the node. If there isn't a straightforward way could I trap the right click button down/up events to achieve this?

Thanks!

5 Answers, 1 is accepted

Sort by
0
Accepted
Kiril Stanoev
Telerik team
answered on 20 May 2010, 08:27 AM
Hi Jerry,

RadContextMenu has a really useful method called GetClickedElement<T>() that returns the item that is being clicked.

private void ContextMenuOpened(object sender, RoutedEventArgs e)
{
    RadContextMenu menu = sender as RadContextMenu;
    if(menu != null)
    {
        RadTreeViewItem clickedItem = menu.GetClickedElement<RadTreeViewItem>();
        if(clickedItem != null)
        {
            clickedItem.IsSelected = true;
        }
    }
}

I've attached a sample project demonstrating this functionality. Give it a try and let me know how it works for you.

All the best,
Kiril Stanoev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
JDT
Top achievements
Rank 1
answered on 20 May 2010, 04:53 PM
Worked great thanks.
0
Tina Stancheva
Telerik team
answered on 20 May 2010, 05:36 PM
Hi JDT,

We are glad we were able to help.

Please let us know if we can further assist you.

Kind regards,
Tina Stancheva
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
JDT
Top achievements
Rank 1
answered on 21 May 2010, 06:45 PM
Here's a quick code snippet of what I had to do to get right click and multiselect to play nice. In case anyone needs it.

ObservableCollection<RadTreeViewItem> lastItemsClicked = new ObservableCollection<RadTreeViewItem>(); 


void contextMenuFolder_Opened(object sender, RoutedEventArgs e) 
        { 
            RadContextMenu rcm = sender as RadContextMenu; 
            if (rcm != null
            { 
                RadTreeViewItem clickedItem = rcm.GetClickedElement<RadTreeViewItem>(); 
                if (clickedItem != null
                { 
                    if (!lastItemsClicked.Contains(clickedItem)) 
                    { 
                        foreach (RadTreeViewItem ris in lastItemsClicked) 
                            ris.IsSelected = false
                        lastItemsClicked.Clear(); 
                    } 
                     
                    clickedItem.IsSelected = true
                } 
            } 
        } 

0
Bobi
Telerik team
answered on 25 May 2010, 02:02 PM
Hi JDT,

Thank you very much for the helpfull example.
Any suggestion and feedback is highly appreciated.

Best wishes,
Bobi
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
TreeView
Asked by
JDT
Top achievements
Rank 1
Answers by
Kiril Stanoev
Telerik team
JDT
Top achievements
Rank 1
Tina Stancheva
Telerik team
Bobi
Telerik team
Share this question
or