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

[Solved] Adding a Context Menu to a tree view item dynamically?

3 Answers 337 Views
Menu
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
donnfelker
Top achievements
Rank 1
donnfelker asked on 22 Dec 2008, 09:34 PM
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!

3 Answers, 1 is accepted

Sort by
0
Ivan
Telerik team
answered on 26 Dec 2008, 09:06 AM
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.
0
shai
Top achievements
Rank 1
answered on 03 Feb 2009, 03:19 PM
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 
0
Ivan
Telerik team
answered on 06 Feb 2009, 02:39 PM
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.
Tags
Menu
Asked by
donnfelker
Top achievements
Rank 1
Answers by
Ivan
Telerik team
shai
Top achievements
Rank 1
Share this question
or