Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top