开发者

Getting the parent node of selected node in RadTreeView?

开发者 https://www.devze.com 2023-02-02 06:51 出处:网络
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 :

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.
}
0

精彩评论

暂无评论...
验证码 换一张
取 消