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