Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVVM selecting a treeview item programmatically

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!

like image 452
Bob Avatar asked Nov 25 '09 15:11

Bob


Video Answer


2 Answers

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.

like image 78
Erin Avatar answered Nov 09 '22 04:11

Erin


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

like image 25
Cameron MacFarland Avatar answered Nov 09 '22 06:11

Cameron MacFarland