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

Problems getting ParentItem

1 Answer 40 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Weverton
Top achievements
Rank 1
Weverton asked on 10 Dec 2009, 11:37 AM
Hi,

I need to get the ParentItem of a RadTreeViewItem. I'm doing like this:

RadTreeViewItem node = new RadTreeViewItem {Header = "Parent Node"}; 
radTreeView.Items.Add(node); 
RadTreeViewItem childNode = new RadTreeViewItem {Header = "Child Node"}; 
node.Items.Add(node); 

However, if I try childNode.ParentItem I get a null value, and If try childNode.RootItem, I always get the node it self.

How can I get these both properties correctly??

Thanks,

Weverton

1 Answer, 1 is accepted

Sort by
0
Kiril Stanoev
Telerik team
answered on 14 Dec 2009, 02:35 PM
Hello Weverton,

The reason why you get null is because the items you add have not been prepared yet i.e. their parent property has not been set. To get the immediate parent of an item, you can use the following code snippet:

RadTreeViewItem parentItem = Telerik.Windows.Controls.ItemsControl.ItemsControlFromItemContainer(this.childNode) as RadTreeViewItem;

If you want to get the root item, you can use the above code in a recursive method:

private RadTreeViewItem GetRootItem(RadTreeViewItem item)
{
    RadTreeViewItem parentItem = Telerik.Windows.Controls.ItemsControl.ItemsControlFromItemContainer(item) as RadTreeViewItem;
    if (parentItem != null)
    {
        return this.GetRootItem(parentItem);
    }
    else
    {
        return item;
    }
}

I am attaching my test project for further reference. Have a look at it and let me know if you have additional questions.


Greetings,
Kiril Stanoev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
TreeView
Asked by
Weverton
Top achievements
Rank 1
Answers by
Kiril Stanoev
Telerik team
Share this question
or