Adding a Context Menu to a tree view item dynamically?

4 posts, 0 answers
  1. donnfelker
    donnfelker avatar
    1 posts
    Member since:
    Apr 2007

    Posted 22 Dec 2008 Link to this post

    Has anyone added a context menu to a treeview item dynamically?

    I only want to add the context menu if the item is of a certain type. I'm currently working with the ItemPrepared event.
    Like this:
     private void RuleTree_ItemPrepared(object sender, Telerik.Windows.Controls.RadTreeViewItemPreparedEventArgs e)  
    {  
        if (e.PreparedItem.Item is theTypeThatImLookingFor)  
        {  
            // Add an icon.   
            e.PreparedItem.DefaultImageSrc = "../../Resources/Icons/add.png";  
     
            // Build menu  
            RadContextMenu menu = new RadContextMenu();  
            menu.EventName = "MouseLeftButtonDown";  
              
            foreach (var name in EnumExtensions.GetNames(typeof (AnEnumeration)))  
            {  
                RadMenuItem menuItem = new RadMenuItem  
                                           {  
                                               Header = name.ToString(),  
                                           };  
                menuItem.Click += OnContextClick;   
                menu.Items.Add(menuItem);  
            }  
              
            // TODO: Figure out how to add menu to RadTreeViewItem  
        }  

    How do I add the context menu to the tree view item? I havent found any examples and it isnt exactly obvious (IMO). Thanks!
  2. Ivan
    Admin
    Ivan avatar
    519 posts

    Posted 26 Dec 2008 Link to this post

    Hi donnfelker,

    Thank you for contacting Telerik Support.

    Please try the code below:
    private void RuleTree_ItemPrepared( 
       object sender, 
       Telerik.Windows.Controls.RadTreeViewItemPreparedEventArgs e) 
       RadContextMenu menu = new RadContextMenu(); 
     
       menu.EventName = "MouseLeftButtonUp"
       menu.ModifierKey = ModifierKeys.Control; 
     
       menu.Items.Add(new RadMenuItem() { Header = "item 1" }); 
       menu.Items.Add(new RadMenuItem() { Header = "item 2" }); 
       menu.Items.Add(new RadMenuItem() { Header = "item 3" }); 
     
       RadContextMenu.SetContextMenu(sender as FrameworkElement, menu); 

    Please note there are some limitations with the MouseButtonDown in the TreeView so please use the button's UP event.

    Let me know if there are other questions.

    Regards,
    Ivan
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. DevCraft banner
  4. shai
    shai avatar
    7 posts
    Member since:
    Feb 2009

    Posted 03 Feb 2009 Link to this post

    Hi
    I added the below code to mine and i registered to the ItemPrepared event but i still cant see the context menu
    any advice??

    you have very freindly Great controls 
    Thanx 
  5. Ivan
    Admin
    Ivan avatar
    519 posts

    Posted 06 Feb 2009 Link to this post

    Hello shai,

    Please note that we are using the mouse's Up event because there are some limitations with the Down event. If you hook on the Down event, nothing will happen.

    The other point you should be aware of - in the code bellow we filtered the menu to show only when the Control key is pressed.

    If nothing of the above helps please send us a sample application for further investigation.

    Regards,
    Ivan
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Back to Top