MVVM – Binding TreeView Item Changed to ICommand

After browsing the web for more tha an hour there were quite some post which helped me to get a clue on how to bind a Tree View item selected.
All these posts gave me call back, on which I had to fire an event again, and add a subscription to it.

I wanted that call back subscription to be an ICommand, so that it is in sync with MVVM model.
But to bring the Call back into the View model had to go an extra mile.

You need a dependency property which would hold the Tree View Item Selected value.
This property should be always updated with the New value, so that you would get an call back on SelectedItemChanged when a new item in the tree is selected.

Share this:

Like this:

About jacob aloysiousI'm a 26 yrs old developer working with Atmel R&D India Pvt. Ltd., Chennai.
I'm currently working in C#, have done a bit of Java programming too. And also an Android enthusiast.
Like spending my spare time @ SO : http://stackoverflow.com/users/1218809/jacob-aloysious

7 Responses to MVVM – Binding TreeView Item Changed to ICommand

Great work on this. I’ve got a slight problem that you may be able to help with. I’ve got my treeview in a tabcontrol but I’m finding if I switch tabs that when I go back the treeview is reset visually but it does remember the CurrSelItem. My question is whether there’s a way to get the treeview to expand to show what the selected item is.

Great work. Helped me to save a lot of time. Unfortunally the provided download doesnt work. The property CurrselItem is never set. After a little bit investigation I found the reason: The binding must be declared explicitly as TwoWay.
ViewModelUtils:TreeViewHelper.SelectedItem=”{Binding CurrSelItem, Mode=TwoWay}”

>> This piece of code in SelectedItemChanged in TreeeViewHelper causes adding a new event handler each time a tree view item is clicked.
Agree! But I guess the solution should be object specific and not global (static).
May be Unsubscribing before Subscription could be an idea..

I’d been recommended this website by means of my nephew. I’m not sure whether it organize can be authored by means of your ex since no one recognize like accurate pertaining to the issues. You will be superb! Thanks a lot!