将 TreeView 和节点分开
我有一个关于良好应用程序设计的问题,这个问题已经困扰我好几天了。人们可以将其描述为:
- 我有一个由节点组成的树结构,这些节点保存对其子节点的引用。
- 我现在想在 TreeView 中显示该结构(我是实现 TreeView 的人)。
- 我不希望节点“知道”有一个 TreeView,这使我能够将这两个组件分开。
那么我在哪里存储节点是否展开的信息(该信息不是节点本身的一部分)。
关于如何以干净的方式实现这一点有什么想法吗?
谢谢,
科内
I have a problem concerning good application design that has been keeping me up for days now. One can describe it as:
- I have a tree structure made up of Nodes holding references to their children.
- I would now like to display that structure in a TreeView (I am the one who will implement the TreeView).
- I don't want the Nodes to 'know' that there is a TreeView, enabling me to keep those two components separate.
So where do I store the information whether an node is expanded or not (this information is not part of the Node itself).
Any idea on how to implement this in a clean way?
Thanks,
Konne
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建一个绑定视图和实际模式的 TreeViewModal 类。显然,这被称为 Model/View/ViewModel 模式。 这里是 TreeView 的示例。这两个链接都有 C# 代码,但很容易移植到任何语言。
Create a TreeViewModal class that binds the view and the actual modal. Apparently this is known as Model/View/ViewModel pattern. Here is an example for the TreeView. Both the links has code in C# but it is easy to port to any language.
如果每个节点都有唯一的可散列键,您可以将散列表添加到 TreeView 以包含扩展节点的键。
If each node has a unique hashable key you could add a hash table to
TreeView
to contain the keys of the expanded nodes.