在wpf treeview中,如何用以前的扩展值重绘节点(使用MVVM)
我正在开发一个具有严格 MVVM 模式的 wpf 桌面应用程序。
目前我的应用程序正在执行以下操作:
- 使用 HierarchicalDataTemplate 显示树视图。
- 用户可以展开或折叠节点。
- 用户可以添加新节点(拖放+双击)。
- 每次添加新节点时,模型都会更新,并且树视图会根据模型重新创建。
- 由于重新创建了Treeview,所以添加节点后所有节点都显示为展开的。
我想显示节点及其之前的扩展状态。有没有办法使用 MVVM 来做到这一点?到目前为止我想到的是
- 模型不应包含任何与如何绘制 UI 相关的数据?
- VM 应该只从模型获取数据并将其放入 UI(并将日期从 UI 传递到模型)??
谢谢你的想法。我可能离铁路还很远。但只是想从你们那里得到一些智慧。
谢谢
派雅
I am developing a wpf desktop app with strict MVVM pattern.
Current my app is doing following things:
- Showing a Treeview with HierarchicalDataTemplate.
- User can expand or collapse Nodes.
- User can add add new Nodes(Drag n Drop + double click).
- Everytime a new Node is added Model is updated and Treeview is recreated based on Model.
- Because Treeview is recreated, all nodes are shown as expanded after adding nodes.
I want to show nodes with their previous expanded condition. Is there any way to do this using MVVM ? What I have thought so far is
- Model should not contain any data related to how to draw UI ??
- VM should just get data from Model and put it in UI(and pass date from UI to Model) ??
Thanks for your thoughts. I may be way far out form rail. But just want to have some wisdom from you guys.
Thanks
PAIJA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您还没有阅读过 Josh Smith 撰写的这篇精彩文章:通过以下方式简化 WPF TreeView使用 ViewModel 模式
基本上,他建议在虚拟机中包含一个名为
IsExpanded
的属性,并将TreeView
正确绑定到它,以便展开/折叠状态完全由程序员控制。If you haven't already, read this great article by Josh Smith: Simplifying the WPF TreeView by Using the ViewModel Pattern
Basically what he suggests there is to include a property called
IsExpanded
in your VM and bind theTreeView
to it correctly so that the expanded/collapsed status is entirely controlled by the programmer.我认为可能的一种解决方案是停止树的重新创建,只需更新模型并仅将节点项添加到要删除它们的当前节点。只需刷新模型中的集合,而不刷新树。如果这不适合您的建筑师,请告诉我们。
谢谢,
贾格德夫·乔桑
One solution what I think could be is to stop the recreation of the tree, just update the model and only add nodeitems to the current node where you are dropping them. Just refresh the collections in model and dont refresh the tree. Let us know if this does't suits your architect.
Thanks,
Jagdev Josan
视图模型可以包含视图相关信息,这就是它的用途。它是纯粹商业和纯粹景观之间的桥梁。我的视图模型通常公开对象的一些业务属性并添加一些相关的视图属性。如果您需要的只是业务属性,那么直接绑定到业务层。只有当您需要做类似您的情况的事情时,您才需要视图模型。
如果您想完全重新创建树(这听起来很疯狂),您可以将节点的展开状态存储在视图模型中,并使用 ItemsContainerStyle 将它们绑定到树视图项。这样,当您重新创建树视图时,之前展开的节点仍将展开。
因此,您包装的业务对象将包含一个额外的属性 IsExpanded,您可以使用它来恢复树视图状态。
另外,我是否提到过重新创建树视图有点过头了?
The view model can contain view related information, that is what it is for. It is a bridge between pure business and pure view. My view models usually expose a few business properties of an object and add a few related view properties. If all you need is business properties, then bind straight to the business layer. Its only when you need to do something like your situation here that you need a view model.
If you want to completely recreate the tree (which sounds crazy) you can store the expanded state of the nodes in your view model and bind them to the tree view items using an ItemsContainerStyle. that way when you recreate your tree view your previously expanded nodes will still be expanded.
So your wrapped business objects will contain an extra property IsExpanded that you can use to restore your tree view state.
P.s. did i mention its a bit over the top to recreate the tree view?