TreeView ParentItem property

7 posts, 0 answers
  1. andrew rekvils
    andrew rekvils avatar
    18 posts
    Member since:
    Dec 2005

    Posted 18 May 2009 Link to this post

    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? 

  2. Valentin.Stoychev
    Admin
    Valentin.Stoychev avatar
    2198 posts

    Posted 19 May 2009 Link to this post

    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.
  3. DevCraft banner
  4. andrew rekvils
    andrew rekvils avatar
    18 posts
    Member since:
    Dec 2005

    Posted 19 May 2009 Link to this post

    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?
  5. Valentin.Stoychev
    Admin
    Valentin.Stoychev avatar
    2198 posts

    Posted 20 May 2009 Link to this post

    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.
  6. Rich
    Rich avatar
    20 posts
    Member since:
    Feb 2004

    Posted 20 Sep 2009 Link to this post

    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.
  7. Miroslav
    Admin
    Miroslav avatar
    922 posts

    Posted 23 Sep 2009 Link to this post

    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.
  8. Rich
    Rich avatar
    20 posts
    Member since:
    Feb 2004

    Posted 23 Sep 2009 Link to this post

    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
Back to Top
DevCraft banner