我应该如何将 TreeView 控件中的分层数据公开给 MVP 演示器?

发布于 2024-07-25 09:10:51 字数 569 浏览 4 评论 0原文

我在 Winforms TreeView 控件中有一些分层数据,我需要将其公开为属性,以便我的演示者可以同步对其所做的更改。 需要明确的是,我使用的是被动视图模式。 对于大多数 WinForm 控件来说,这是理所当然的。 控件本身将其数据公开为系统类型,可以轻松传递给演示者。 由于其复杂性,TreeView 不容易映射到系统类型。 我寻找框架集合类型,但树似乎被忽略了。 (微软似乎在几个更高级别的类的底层使用了树结构,但选择不创建通用Tree类系列。)

无论如何,我试图公开的数据永远不会更多深度超过两个节点,所以我什至不确定是否要继续使用 TreeView。 更有理由避免将演示者绑定到 TreeNodeCollection。

我已经提出了一些想法,但我只是想将它们从 SO 社区中反弹出来:

  • 将树公开为集合的集合......比如说列表字典。
  • 创建自定义树结构,在流程中将依赖项引入到视图中。

顶级节点平均有 5-10 个条目,而其子节点理论上可以达到 50 个条目,但实际上不会超过 3 或 4 个。

有什么建议吗?

I have some hierarchical data in a Winforms TreeView control and I need to expose it as a property so my presenter can synchronize changes to it. Just to be clear, I'm using the Passive View pattern. With most WinForm controls this is a no-brainer. The controls themselves expose their data as a system type which can easily be passed along to the presenter. TreeViews, because of their complexity don't map easily to a system type. I looked for a framework collection type but tree's seem to have been overlooked. (Microsoft appears to use tree structures underneith the hood for several higher level classes but chose not to create a family of general purpose Tree classes.)

In any case the data I'm trying to expose is never more than two nodes deep so I'm not even sure I'm going to continue using TreeView. All the more reason to avoid tying the presenter to a TreeNodeCollection.

I've already come up with a few ideas but I just wanted to bounce them off the SO community:

  • Expose the tree as a collection of collections... say a dictionary of lists.
  • Create a custom tree structure, introducing a dependency into the view in the process.

The top-level nodes will average 5-10 entries while their child nodes could theoretically hit counts as high as 50 but in practice won't go over 3 or 4.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心凉怎暖 2024-08-01 09:10:53

如果这是一个依赖于性能的应用程序,我建议不要使用基于树的集合

If this is a performance dependent app, i'd advise agianst the use of tree based collections

小姐丶请自重 2024-08-01 09:10:52

以下是三个免费 .NET 库的链接,其中包含树集合实现:

我对其中任何一个都没有经验。

Here are links to three free .NET libraries that contain tree collection implementations:

I don't have experience with any of them.

你在看孤独的风景 2024-08-01 09:10:51

我认为你必须朝相反的方向思考:演示者应该如何向视图公开层次结构。 而且您应该以抽象的方式考虑视图,而不是过多地关注具体的实现(在您的例子中为 TreeView)。 通常,当您开始使用 MVP 模式实现内容时,您将开始编写演示者逻辑,同时定义视图界面。 理想情况下,您可以使用 TDD 和视图的模拟实现来完成此操作。 只有这样您才能实现具体视图(Windows 窗体或控件)。

所以:您必须创建一个模型,然后将其传递给视图。 我建议实现自定义层次结构(请参阅复合模式)。

在您的情况下,视图的实现可以使用 TreeViewNode 的 Tag 属性在 TreeView 的节点和模型中的元素之间进行映射。

I think you have to think in the opposite direction: how should presenter expose a hierarchical structure to a view. And you should think about the view in abstract terms and not concentrate too much on the concrete implementation (TreeView in your case). Typically when you start implementing stuff using the MVP pattern, you will start writing presenter logic, at the same time defining the view interface. Ideally you would do this using TDD and a mock implementation of the view. Only then you would implement a concrete view (Windows form or a control).

So: you have to create a model which will then be passed to the view. I would suggest implementing a custom hierarchical structure (see Composite pattern).

In your case the implementation of the view could use the Tag property of the TreeViewNode to map between TreeView's nodes and the elements in your model.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文