Bind TreeView.SelectedItem to a property in the ViewModel in a WinRT app

494 Views Asked by At

In my Windows Store XAML app I’m using the TreeView control from the WinRTXamlToolkit and I’m attempting to two-way bind the SelectedItem property to a property on a ViewModel.

Out of the box, the SelectedItem property is read only and this makes sense because the control supports Virtualization.

I have seen some folk work around this with things like attached properties, helper methods and so forth, a great example of which is seen in this question

WPF MVVM TreeView SelectedItem

But all of the questions/solutions are not based on WinRT and all of my attempts to rework the solution code for a WinRT app have proven fruitless.

So, my question is, is this possible in a WinRT app? What am I missing? Thanks

1

There are 1 best solutions below

0
On

I'd skip trying to come up with a bindable property globally for the view model and instead use the IsSelectedBindingPath and IsExpandedBindingPath properties of the TreeView as in the debugging tools' example of the control's usage. Then when you want to select/expand an item from the view model - use a method similar to SelectItem() in my view model where I essentially set IsExpanded/IsSelected to true in item/node view models throughout the path from the root of the view model tree and load the content of the tree if the nodes in the expected path do not exist.