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

RadContextMenu applied recursively on RadTreeViewItem nodes

2 Answers 96 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Domagoj
Top achievements
Rank 1
Domagoj asked on 06 Jul 2011, 09:44 AM
Hi!

I have a problem applying a RadContextMenu to different nodes in the TreeView.

Specific items in my tree need to have a context menu, others should ignore right-click on them.
Each node that should have a menu should be able to have its own menu, dynamically created.
For purposes of this example, each menu will have only a single item, some random number.

There is a method like this:

private void createTemplateGroupContextMenu(RadTreeViewItem groupNode)
{
    RadContextMenu menu = new RadContextMenu();
    menu.Items.Add(new RadMenuItem() { Header = rand.Next().ToString() });
    RadContextMenu.SetContextMenu(groupNode, menu);
}

For each node that needs to have a menu, I am calling createContextMenu(node);

However, what happens is that the child nodes of such nodes are using the same context menu. I don't want the child nodes to have a context menu at all - in other words - I want to have a context menu only for nodes that I explicitely set.

How can this be achieved?

2 Answers, 1 is accepted

Sort by
0
Accepted
Petar Mladenov
Telerik team
answered on 11 Jul 2011, 08:53 AM
Hi Domagoj,

In order to avoid applying the context menu to the children RadTreeViewItems, you can apply it to the Header of the Item. For example if the tree is declaratively defined, you can do like so:
<telerik:RadTreeViewItem>
                  <telerik:RadTreeViewItem.Header>
                      <TextBlock Text="Item C">
                           <telerik:RadContextMenu.ContextMenu>
                              <telerik:RadContextMenu >
                                  <telerik:RadMenuItem Header="Copy" />
                                  <telerik:RadMenuItem Header="Paste" />
                                  <telerik:RadMenuItem Header="Cut" />
                                  <telerik:RadMenuItem IsSeparator="True" />
                                  <telerik:RadMenuItem Header="Select All" />
                              </telerik:RadContextMenu>
                          </telerik:RadContextMenu.ContextMenu>
                      </TextBlock>
                  </telerik:RadTreeViewItem.Header>                  
 
                  <telerik:RadTreeViewItem Header="Item C.1" />
                  <telerik:RadTreeViewItem Header="Item C.2" />
              </telerik:RadTreeViewItem>

If the RadTreeView is DataBound to HIerarchy of object using telerik:HIerarchicalDataTempalates, similarly, you add the ContextMenu in the Template for the spepcific hierarchy level:
<telerik:HierarchicalDataTemplate x:Key="Organization"
                                          ItemsSource="{Binding Departments}"
                                          ItemTemplate="{StaticResource Department}">
            <TextBlock Text="{Binding Name}">
                <telerik:RadContextMenu.ContextMenu>
                    <telerik:RadContextMenu ItemClick="RadContextMenu_ItemClick">
                        <telerik:RadMenuItem Header="New Department" />
                    </telerik:RadContextMenu>
                </telerik:RadContextMenu.ContextMenu>
            </TextBlock>
        </telerik:HierarchicalDataTemplate>
Now suppose your tree is databound, but you want to use ContextMenus only for specific items in the same level. Then you can use TemplateSelectors to direct the different types of busines objects / ViewModels  to the different HierarchicalTemplates (that contain the different ContextMenus needed).
Please let us know if you need further assistance on this.

All the best, Petar Mladenov
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Domagoj
Top achievements
Rank 1
answered on 12 Jul 2011, 10:05 AM
I needed a programmatical way to achieve the result, but you have given me enough information to do it myself.

The problem I was facing is that I was setting the RadTreeViewItem.Header to the string, which is not a FrameworkElement. I have wrapped the string into a textblock so I can set the Context menu.
node.Header = new TextBlock() { Text = text };

Now it works.

Thanks for the reply.
Tags
Menu
Asked by
Domagoj
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
Domagoj
Top achievements
Rank 1
Share this question
or