RadContextMenu applied recursively on RadTreeViewItem nodes

3 posts, 1 answers
  1. Domagoj
    Domagoj avatar
    17 posts
    Member since:
    Jun 2011

    Posted 06 Jul 2011 Link to this post


    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. Answer
    Petar Mladenov
    Petar Mladenov avatar
    3154 posts

    Posted 11 Jul 2011 Link to this post

    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:
                          <TextBlock Text="Item C">
                                  <telerik:RadContextMenu >
                                      <telerik:RadMenuItem Header="Copy" />
                                      <telerik:RadMenuItem Header="Paste" />
                                      <telerik:RadMenuItem Header="Cut" />
                                      <telerik:RadMenuItem IsSeparator="True" />
                                      <telerik:RadMenuItem Header="Select All" />
                      <telerik:RadTreeViewItem Header="Item C.1" />
                      <telerik:RadTreeViewItem Header="Item C.2" />

    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 ItemClick="RadContextMenu_ItemClick">
                            <telerik:RadMenuItem Header="New Department" />
    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!

  3. Domagoj
    Domagoj avatar
    17 posts
    Member since:
    Jun 2011

    Posted 12 Jul 2011 Link to this post

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