Getting the parent node of selected node in RadTreeView?

I have a RadTreeView and a lot of nodes in this treeview. what i want to do is, when a node clicked i want to get the top parent node. How can i do this? the structure is like this :

  1. Parent
    1. Child 3.Child's child 4.selected node 3.Child's child 3.Child's child

when i select the "4.selected node" i want to be able to get the top parent node not by calling the method three times.


private void radTreeView_SelectionChanged( object sender, Telerik.Windows.Controls.SelectionChangedEventArgs e )
   // Get a reference to the treeview
   Telerik.Windows.Controls.RadTreeView treeView = sender as Telerik.Windows.Controls.RadTreeView;
   // Get the currently selected items
   ObservableCollection<Object> selectedItems = treeView.SelectedItems;
   RadTreeViewItem item = selectedItems[ 0 ] as RadTreeViewItem;
   // Get the previous item and the previous sibling item
   RadTreeViewItem previousItem = item.PreviousItem;
   RadTreeViewItem previousSiblingItem = item.PreviousSiblingItem;

   // Get the next item and the next sibling item
   RadTreeViewItem nextItem = item.NextItem;
   RadTreeViewItem nextSiblingItem = item.NextSiblingItem;

   // Get the parent item and the root item
   RadTreeViewItem parentItem = item.ParentItem;
   RadTreeViewItem rootItem = item.RootItem;

Mainly the last line of this entire event would be what you need I reckon, more information regarding this can be found here. However this is for WPF version of the radtreeview.

I just wrote this method for you ,as Im now working on huge project with Rad Controls :

public void FindFather(RadTreeNode n)

            foreach (RadTreeNode node in RadTreeView1.GetAllNodes())
                if (node.Level == 0 && !node.GetAllNodes().Contains(node))
                    //this is the node that your looking for 
                    //do something with it
                    Console.Write("this is the Top Parent");


Hope u like it bro, never give up when coding

@King A.Majid: I don't think your logic is correct. What about the parameter "RadTreeNode n"?

My method is just simple as follow:

public RadTreeNode CwGetTreeNodeTopParent(RadTreeNode nodeToFindTopParent)
            var parentNode = nodeToFindTopParent.ParentNode;
            if (parentNode.Level == 0)
                return parentNode;
            parentNode = CwGetTreeNodeTopParent(parentNode);
            return parentNode;

You should check the node (which is to find the top parent) to make sure it is not the highest level.

Eg, we want to find the top parent for selectedNode:

if(selectedNode.Level != 0)
var topParent = CwGetTreeNodeTopParent(selectedNode);
//To do something here.

Need Your Help

Refreshing a fragment from a DialogFragment

android android-fragments android-viewpager android-dialogfragment

I've been going around in circles trying to do something that seems pretty basic. I have a DialogFragment that accepts a users input, then, on submission, refreshes a ListView in a Fragment that i...

PHP timeslot booking

php sql timeslots

regarding of this question..

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.