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

Adding context menu with Hiearchical View Model

3 Answers 79 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Anne Lyon
Top achievements
Rank 1
Anne Lyon asked on 23 Sep 2010, 02:38 PM
Hi,

My TreeView is bound to a ObservableCollection<TreeNode>. Using a HierarchicaldataTemplate the ItemSource is bound to my collection's TreeNodeChildren.
I want to bind a context meny to each node, and must do it in code as the context meny can be different for each node.

But how do I bind the contect menu to the node when the node is a TreeNode and not a RadTreeViewItem?
I cannot cast the current TreeNode to a RadTreeViewItem and hence cannot use the code:
myRadTreeViewItem.SetValue(RadContextMenu.ContextMenuProperty, contMenu);
as TreeNode doesn't have that functionality. I know how to bind properties, like isExpanded, between myTreeNode class and the TreeView, but how do I "bind" a command?

So in code I want to add a new TreeNode to my bound TreeNodeChildren collection, then dynamically create a new context menu and attach this context menu to my newly created TreNode. How do I do that?

Appreciate if somebody has an idea!


3 Answers, 1 is accepted

Sort by
0
Petar Mladenov
Telerik team
answered on 24 Sep 2010, 06:09 PM
Hello Anne ,

Could you please explain me  by what criteria you are going to differentiate  the context menus? Is it going to be a property, or the type of the business object, or something else?

Best wishes,
Petar Mladenov
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
Anne Lyon
Top achievements
Rank 1
answered on 27 Sep 2010, 01:23 PM
Hello Petar,

First of all I apologize this question has appeared so many times in the forum as I encountered a Telerik server error which made me believe the postings had been unsuccessful... Please remove duplicate postings if you like...

When the user clicks a node in my treeView my code behind accesses a database and returns a list of strings the context menu will consist of depending on the properties of the current node. This context menu allows the user to add sub-nodes, i.e. build a hierarchy. (I.e. if the current node is a Financial Department you can add "Book-keeping", "Auditing", "Billing"  from a context menu- if the current Node is a Technical Department you can add "Design", "Development", "Patenting" etc. This cannot be hardcoded but must be fecthed from the database on the go)
I have tried the code:
RadContextMenu.SetContextMenu(myTreeN ode, contMenu)   
and I have tried:
RadContextMenu.SetContextMenu(myTreeView.SelectedItem, contMenu) 
but I get a syntax error on first parameter as it is not of the right type.
How do I attach a context menu to myTreeNode or how do I find the RadTreeViewItem corresponding to my current node so that I can attach a context menu?
0
Petar Mladenov
Telerik team
answered on 28 Sep 2010, 02:57 PM
Hello Anne ,

I am not sure when do you attach the ContextMenu. However, a possible approach towards your scenario is to handle the RadTreeView ItemPrepared() event to define a ContextMenu for each item.

Please take a look at the attached example and let me know if it works for you. If it doesn't or if your scenario requires a different approach, please elaborate a bit more on it.

You can also check this demo implementing ContextMenu on the RadTreeView : http://demos.telerik.com/silverlight/#TreeView/AddRemoveDisable

Let us know if we can further assist you.

Sincerely yours,
Petar Mladenov
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
Anne Lyon
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
Anne Lyon
Top achievements
Rank 1
Share this question
or