Thank you Hristo, this helped me solve the problem.
Just so everyone is aware, the method presented by Josh Smith in my post above will not work with the RadTreeView. This is because the items of the tree that are initially collapsed are not actually instantiated, so therefore the selected event will never be registered. To work around this, I used a GalaSoft MVVM messenger event instead, and handled the message in a behavior that brings the item into view. Here is the general code I used.
First, I created a behavior in the View namespace:
public class TreeSelectionBehavior : Behavior<FrameworkElement>
{
Messenger messenger = Messenger.Default;
protected override void OnAttached()
{
base.OnAttached();
messenger.Register<GalaSoft.MvvmLight.Messaging.DialogMessage>(this, Identifier, ItemSelected);
}
public string Identifier { get; set; }
public MyTreeGenericNode SelectedNode
{
get { return (MyTreeGenericNode)GetValue(SelectedNodeProperty); }
set { SetValue(SelectedNodeProperty, value); }
}
// Using a DependencyProperty as the backing store for the selected node object.
public static readonly DependencyProperty SelectedNodeProperty =
DependencyProperty.Register("SelectedNode", typeof(MyTreeGenericNode), typeof(TreeSelectionBehavior), new UIPropertyMetadata(null));
public RadTreeView TreeView
{
get { return (RadTreeView)GetValue(TreeViewProperty); }
set { SetValue(TreeViewProperty, value); }
}
// Using a DependencyProperty as the backing store for the RadTreeView.
public static readonly DependencyProperty TreeViewProperty =
DependencyProperty.Register("TreeView", typeof(RadTreeView), typeof(TreeSelectionBehavior), new UIPropertyMetadata(null));
public void ItemSelected(GalaSoft.MvvmLight.Messaging.DialogMessage dm)
{
Assert.IsNotNull(SelectedNode);
Assert.IsNotNull(TreeView);
TreeView.BringPathIntoView(SelectedNode.DisplayPath);
}
}
The behavior is added to the XAML and bound to the selected item and tree. The reference to i: is for the System.Windows.Interactivity namespace. The reference to v: is for the View namespace:
Now, in the view model, I message the behavior when the selection is updated:
Make sure to unregister the message in the cleanup of the view:
One final note - the node class that is used as items source must have a ToString() override that represents the name of the path element for the node: