简体   繁体   English

MVVM以编程方式选择树视图项

[英]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. 我有一个包含ObservableCollection对象的VM,而不是每个对象的VM。 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. 因此我不认为我可以使用经常讨论的SelectedItem bool,因为我不认为我可以绑定到集合对象上的属性...只是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. 所以我已经完成了很好的编写,没有代码隐藏和最小耦合,但是当一个新项目被添加到集合中,绑定到treeView时,我需要选择该项目。

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. 你应该为树视图的每个元素构建一个包装器,它上面有IsSelected bool以及IsExpanded bool,它们使生命更容易显示数据。 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. Josh Smith有一篇关于CodeProject的文章,他建议创建一个ViewModel对象来表示TreeView的每个节点,然后根据需要自动对其进行自动装配。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM