Right Click Context Menu Problem

6 posts, 1 answers
  1. JDT
    JDT avatar
    45 posts
    Member since:
    Jul 2007

    Posted 20 May 2010 Link to this post

    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!
  2. Answer
    Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1512 posts

    Posted 20 May 2010 Link to this post

    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.
  3. DevCraft banner
  4. JDT
    JDT avatar
    45 posts
    Member since:
    Jul 2007

    Posted 20 May 2010 Link to this post

    Worked great thanks.
  5. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 20 May 2010 Link to this post

    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.
  6. JDT
    JDT avatar
    45 posts
    Member since:
    Jul 2007

    Posted 21 May 2010 Link to this post

    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
                    } 
                } 
            } 

  7. Bobi
    Admin
    Bobi avatar
    513 posts

    Posted 25 May 2010 Link to this post

    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.
Back to Top
DevCraft banner