Expanding databound tree items

4 posts, 0 answers
  1. Fred
    Fred avatar
    5 posts
    Member since:
    Mar 2011

    Posted 08 Mar 2011 Link to this post

    I have a completely dynamic RadTreeView that users can add items to. To add a child item, the user selects an existing item and presses a button to generate a new item under the currently selected item. All of that works fine, but the parent item doesn't expand to show the newly added item. I'm trying to get this to happen so that the user doesn't have to manually expand the parent after adding a child.  

    This project is using MVVM and the tree items are all databound to an ObservableCollection of a custom class located in my View-Model. The currently selected item is also databound to a property in my View-Model that is of the same custom class as the ObservableCollection. 

    Since I'm adding the new items to my tree through the databound property in the View-Model, I can't simply tell the parent to expand from there because it has no access to the UI elements. Immediately after I create the new item I set it in the View-Model as the currently selected item. I figure this will trigger the SelectionChanged event on the RadTreeView (which it does)  so my idea is to try and expand the parent of the currently selected item from there. 

    The problem there is that I can't get anything working properly in the SelectionChanged event.

    The "SelectionChangedEventArgs" Source property only gives me an object of the custom class type. Obviously that doesn't allow me to change the IsExpanded property. Whenever I try to cast the Source property as a RadTreeViewItem it always returns null. Similarly I have also tried iterating through the RadTreeView itself, but that does the same thing. I can easily get the object of the custom class from the View-Model, but nothing I try gives me the RadTreeViewItem for me to expand. 

    I've messed around with the GetItemByPath and ExpandItemByPath methods, but it is completely possible for items in my tree to have the exact same name. In that case, how would those methods know which path is the right path? I don't think those will work for me.

    I have also tried the code here, specifically this:

    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;
    }

    However, as I mentioned before, "item" is always coming out null. If I instead cast it to my custom class, item holds the correct data and I can get the parent item, but is not a RadTreeViewItem for me to expand.

    Thanks in advance for any help...

  2. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 11 Mar 2011 Link to this post

    Hello Fred,

    A good starting article on how to use the GetItemByPath method could be found  here.
    On the other hand accessing a RadTreeViewItem via the business object ( ViewModel) inside could be achieved easy with the RadTreeVewi.ItemContainergenerator.ContainerFromItem(object item) method.
    Feel free to ask if you need further assistance on this.

    Best wishes,
    Petar Mladenov
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  3. UI for WPF is Visual Studio 2017 Ready
  4. Fred
    Fred avatar
    5 posts
    Member since:
    Mar 2011

    Posted 14 Mar 2011 Link to this post

    ItemContainerGenerator.ContainerFromItem is what I needed to move forward.

    Unfortunately that just showed me that the SelectionChanged event would not work for me. While it does get fired, it gets fired too early and the RadTreeView has not had its databound itemssource updated with the new RadTreeViewItem. I tried other events, but none would fire off at the appropriate time. 

    I just came across this forum post and the attached examples ended up showing me how to solve my problem. Instead of trying to do it all manually, I ended up adding an IsExpanded property to my ViewModel object and then data binding that to the View through the RadTreeView's ItemContainerStyle property setter. Then I just had to make sure my ViewModel object implemented INotifyPropertyChanged it and worked. 

    It looks like this ended up making the RadTreeView expand/collapse functionality slow down a bit. Anything I should look at to help debug that?

    Thanks for the help.

  5. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 17 Mar 2011 Link to this post

    Hello Fred,

    Expanding a RadTreeViewItem starts the generation of its direct child RadTreeViewItems. If their number is huge and you have too many expand operations, the RadTreeView will be slower indeed. If this is not what you had in mind, do you mean that the expand/ collapse animation is slower? If so, could you please send us a runnable sample cause this is unexpected behavior and we would like to investigate it and provide you with a better suited solution and advice. Thank you in advance.

    Regards,
    Petar Mladenov
    the Telerik team
Back to Top