Insert / delete a node

2 posts, 0 answers
  1. Hamid
    Hamid avatar
    4 posts
    Member since:
    Mar 2012

    Posted 07 Mar 2012 Link to this post


    I have 2 questions about RADTreeView for WPF:

    1) I need to store a value alongside header in my tree nodes. Every tree node will have the follwoing information:
    Record #

    However, only Name will be visible in the tree (stored as Header). Record # will not be visible but should be stored for background processing. Other tools typically have a Value property alongside Header (or Text). Does Telerik's TreeView allow for storing other data elements in a node?

    2: I have a ContextMenu in my tree. Here is the code for the menu's click event:
            private void TreeContextMenu_ItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e)
                // Get the clicked context menu item
                RadMenuItem menuItem = ((RadRoutedEventArgs)e).OriginalSource as RadMenuItem;
                string header = menuItem.Header as string;
                switch (header)
                    case "Add":
                        // code to insert a new node below the highlighted node.
                    case "Delete":
                        // code to delete the highlighted node in the tree.

    A) How can I insert a new node below the currently selected node in the tree?
    B) How can I delete the currently selected node?


  2. Tina Stancheva
    Tina Stancheva avatar
    3299 posts

    Posted 12 Mar 2012 Link to this post

    Hello Hamid,

    I posted a reply in your other thread but I will describe the basic approach for creating a context menu here as well in case anyone else encounters the same issue:

    When you create a ContextMenu you can attach to its Opened event and make sure that the clicked RadTreeViewItem gets selected. That would allow you to base your add/remove logic on the RadTreeView.SelectedItem/SelectedContainer values.

    Another approach that you can use it to define the ContextMenu in the DataTemplate of the RadTreeViewItems and use the DataContext of each item to bind the RadMenuItem.Command property to a custom command. This would allow you to place the add/remove logic in your view models and add/remove items in the business collections.

    Kind regards,
    Tina Stancheva
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Back to Top