开发者

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 di开发者_如何学Cscussed 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!


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜