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

Insert / delete a node

1 Answer 102 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Hamid
Top achievements
Rank 1
Hamid asked on 07 Mar 2012, 07:55 PM

Hello,

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:
Name
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.
                    break;
                case "Delete":
                    // code to delete the highlighted node in the tree.
                    break;
            }
        }

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?

Thanks,
Hamid

1 Answer, 1 is accepted

Sort by
0
Tina Stancheva
Telerik team
answered on 12 Mar 2012, 11:23 AM
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 >>
Tags
TreeView
Asked by
Hamid
Top achievements
Rank 1
Answers by
Tina Stancheva
Telerik team
Share this question
or