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

TreeView ParentItem property

6 Answers 82 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
andrew rekvils
Top achievements
Rank 1
andrew rekvils asked on 18 May 2009, 08:15 PM
If I add new item programmatically using Items collection (ex: parentItem.Items.Add(new 

RadTreeViewItem()) and immediately try to get ParentItem property from this newly added item - it returns null as if there is no parent item. Is that by design or a bug? 

6 Answers, 1 is accepted

Sort by
0
Valentin.Stoychev
Telerik team
answered on 19 May 2009, 07:32 AM
Hello andrew rekvils,

Is the Parent item already rendered? The ParentItem property is initialized when the item is rendered, before that the item container is not created.

Can you give us more info.

Thanks!

Kind regards,
Valentin.Stoychev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
andrew rekvils
Top achievements
Rank 1
answered on 19 May 2009, 02:16 PM
No. The parent item is not rendered yet. As I was writing ParantItem property is retrived right after item is added to Items collection. The use case for that is that one method adds new item to collection and another independantly method traverses collection and analyses parent-child relashiohship. These two methods can be called syncronously one after another in which case second method will not get parent items correctly yet because items are not garanteed to be rendered at least once by then. Is there any way to force update of the parnt-child relashionship?
0
Valentin.Stoychev
Telerik team
answered on 20 May 2009, 05:53 AM
Hello andrew rekvils,

You can't use the ParentItem property before the treeview is in the visual tree, because this is a property of the RadTreeViewItem class and the RadTreeViewItem class is instantiated from the framework only when the item is in the visual tree.

Best wishes,
Valentin.Stoychev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Rich
Top achievements
Rank 2
answered on 20 Sep 2009, 11:34 PM
This is actually a real problem. There is no real reason to not set this that I can see.. If I step in with the debugger, I can set it and my code will work, but since the property is read-only, I can't force it to be set and that also means the level isn't set. I am trying to process the treeview items and walk the tree, the problem is, I can't tell what level I am on or the parent item which would be very useful.
0
Miroslav
Telerik team
answered on 23 Sep 2009, 10:43 AM
Hi Richard,

The reason why the property is not set immediately is rooted in the control lifecycle in items controls in WPF and Silverlight.

Normally all initialization of the container of an item happens in the PrepareContainerForItemOverride which is called during the first layout pass of the control. Technically we can listen for changes in the items collection and set the property right after it has been added (if the item is a container), but we would like to keep initialization of containers consistent, so I cannot promise that this will be changed.

In the case of adding a treeViewItem in the items collection, you can immediately get its parent by:

var parent = System.Windows.Controls.ItemsControl.ItemsControlFromItemContainer(newItem); 

I understand that this is not the same as just accessing the property but hopefully you will be able to use it.

All the best,
Miroslav
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.
0
Rich
Top achievements
Rank 2
answered on 23 Sep 2009, 10:47 AM
This is just very counter intuitive when you have a collection that you are adding items to and after all the initialization has been done in your code for building the data that will go into the tree, you can't rely on the properties until it has been displayed for the first time.

I understand the intent, but it is inconsistent with what you would expect as a developer.

Thanks
Tags
TreeView
Asked by
andrew rekvils
Top achievements
Rank 1
Answers by
Valentin.Stoychev
Telerik team
andrew rekvils
Top achievements
Rank 1
Rich
Top achievements
Rank 2
Miroslav
Telerik team
Share this question
or