tags:

views:

876

answers:

2

It seems that others have had variations on this question, but from what I can tell it hasnt been addressed for folks using collections in a single view model.

I have a VM that contains an ObservableCollection of objects, not a VM for each object. Therefore I dont think I can use the SelectedItem bool that is often discussed because I dont think I can bind to the property on the collection's objects...just the properties on the VM.

So I've got the whole thing pretty well written with no code-behind and minimal coupling, but when a new item is added to the collection, which is bound to the treeView, I need to select that item.

Ideas?

Thanks!

+3  A: 

When thinking about this. You should really build a wrapper for every element of the tree view that has the IsSelected bool on it as well as the IsExpanded bool they make life so much easier for displaying the data. You could even just add them to your class and use them from there.

Erin
Maybe so. I took a middle road though. I went ahead and implemented INotifyPropertyChanged on each of the object types that the VM's ObservableCollection holds and slapped IsSelected and IsExpanded on them. Down side is that my objects now have that goop in them (is that even bad?). Good side is that I didnt have to create that extra layer of VM's that provided me with nothing beyond a place to hold the underlying object and those two properties.
Bob
If that is all you need you may as well do it that way.
Erin
+2  A: 

Josh Smith has an article on CodeProject where he suggests creating a ViewModel object to represent each node of the TreeView, and then autowires them up as needed.

http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx

Cameron MacFarland
yeah thats a good article. Don't shun me, but I think it may be overkill when all you really need to do is wire in a couple UI interactions (expand, select). :)
Bob