具有多个子项的层次树

发布于 2024-12-07 00:31:01 字数 1787 浏览 1 评论 0原文

我正在尝试思考如何使用 MVVM 模式在 WPF 中解决这个问题。

我正在将 win32 树控件转换为 WPF。旧的树控件沿着这些思路使用 Node 类层次结构(BaseNode 是基类,并且以下每个项目都从它继承并稍微扩展): BaseNode、GroupNode、VehicleNode、PersonNode、EquipmentNode、SupplyNodes(等等)

由此看来,GroupNode 将具有一系列子节点,这些子节点可以是一个或多个 GroupNode、一个或多个 VehicleNode 以及一个或多个 PersonNode。

车辆节点将有一个人员节点列表,这些人员节点将是操作车辆的人员。 VehicleNode 还会有一个 PersonNode 列表,这些 PersonNode 是车辆中的乘客。其中每一个都位于标记为“船员”和“乘客”的“虚拟”节点下(尽管它们都是 PersonNode 类型)。

除此之外,VehicleNode 和 PersonNode 各有 EquipmentNodes 和 SupplyNodes。

该树将有几个“组”节点,这些节点将展开以列出该类型的任何项目。

希望这个文本图能够帮助解决说明问题。

  • 地面舰队(GroupNode)
    • 西海岸(组节点)
    • 东海岸(组节点)
      • 卡车 1(车辆节点)
      • 卡车 2(车辆节点)
        • 船员(DummyNode)
          • 给司机开账单(PersonNode)
        • 乘客(DummyNode)
          • 乘客 1(PersonNode)
          • 乘客 2(PersonNode)
            • 设备(DummyNode)
              • 相机(设备节点)
              • 太阳镜(EquipmentNode)
            • 补给品(DummyNode)
              • 苹果(SupplyNode)
              • 水瓶(SupplyNode)
          • 设备(DummyNode)
            • Jack(设备节点)
            • 轮胎铁(EquipmentNode)
          • 补给品(DummyNode)
            • 备胎(供应节点)
      • 人员(DummyNode)
        • 推销员 Tom (PersonNode)
        • 区域经理 Sally(PersonNode)
So the instance of Truck 2 (VehicleNode) has six child nodes (list of BaseNodes) of various types: List children; // {Bill The Driver(PersonNode), Passenger 1 (PersonNode), Passenger 2 (PersonNode), Jack (EquipmentNode), Tire Iron (EquipmentNode), SpareTire (SupplyNode)}

在我们现有的 win32 树中,当我们添加 Truck 2 节点时,我们手动循环子节点并添加虚拟节点(根据需要)和子节点,以根据子列表中的节点类型和可能的属性创建树在子节点上 - PersonNode 上有一个标志来指示它是乘客还是机组人员,因此我们知道要添加的它应该属于哪个父节点。

我正在努力解决如何在树上用 MVVM 方法来表示这一点,从而允许我们保留上述虚拟节点。

任何帮助将不胜感激!

I am trying to wrap my head around how to solve this problem in WPF using an MVVM pattern.

I am converting a win32 tree control into WPF. The old tree control uses a Node class hierarchy along these lines (BaseNode being the base class and each following item inheriting from it and extending slightly):
BaseNode, GroupNode, VehicleNode, PersonNode, EquipmentNode, SupplyNodes (etc, etc.)

Going from this, A GroupNode will have a list of child nodes which could be one or more GroupNodes, one or more VehicleNodes and one or more PersonNodes.

A VehicleNode would have a list of PersonNodes that would be the crew for operating the vehicle. The VehicleNode would also have a list of PersonNodes that would be passengers in the vehicle. Each of these is under a 'Dummy' node labeled Crew and Passengers (though they are both PersonNode Types).

Outside of this, a VehicleNode and PersonNode would each have EquipmentNodes and SupplyNodes.

The tree will have several "Group" nodes that will be expanded to list any items of that type.

Hopefully, this text diagram will help solve illustrate the problem.

  • Ground Fleet (GroupNode)
    • West Coast (GroupNode)
    • East Coast (GroupNode)
      • Truck 1 (VehicleNode)
      • Truck 2 (VehicleNode)
        • Crew (DummyNode)
          • Bill the Driver (PersonNode)
        • Passengers (DummyNode)
          • Passenger 1 (PersonNode)
          • Passenger 2 (PersonNode)
            • Equipment (DummyNode)
              • Camera (EquipmentNode)
              • Sunglasses (EquipmentNode)
            • Supplies (DummyNode)
              • Apple (SupplyNode)
              • Water Bottle (SupplyNode)
          • Equipment (DummyNode)
            • Jack (EquipmentNode)
            • Tire Iron (EquipmentNode)
          • Supplies (DummyNode)
            • SpareTire (SupplyNode)
      • Personnel (DummyNode)
        • Salesman Tom (PersonNode)
        • District Manager Sally (PersonNode)

So the instance of Truck 2 (VehicleNode) has six child nodes (list of BaseNodes) of various types:
List children; // {Bill The Driver(PersonNode), Passenger 1 (PersonNode), Passenger 2 (PersonNode), Jack (EquipmentNode), Tire Iron (EquipmentNode), SpareTire (SupplyNode)}

In our existing win32 tree, when we add the Truck 2 node, we manually cycle through the child nodes and add the dummy nodes (As required) and children to create the tree based on the type of nodes in the children list and potentially a property on the child node - the PersonNode has a flag on it to indicate if its a passenger or crew, so we know which parent node to add it should belong to.

I'm struggling with how this can be represented in an MVVM approach on a tree that would allow us to retain the Dummy Nodes as above.

Any help would be greatly appreciated!

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

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

发布评论

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

评论(1

猫瑾少女 2024-12-14 00:31:01

使用HierarchicalDataTemplate,查看一个不错的示例这里

您可以公开 ViewModel 中的节点列表,并将其绑定到 TreeView 的 ItemsSource。每个节点还将有一个子属性,它是另一个节点列表。

每个节点都可以只是一个简单的类,或者如果您需要在每个节点放置 ICommands/Actions/Methods,则实际上可以是 ViewModel 本身。

每个节点都可以按照您希望的方式在 ViewModel 中的 C# 中进行设置,因为 HierarchicalDataTemplate 会为您完成大量绑定工作。

Use HierarchicalDataTemplate, see a decent example here.

You would expose a list of nodes from your ViewModel, and bind that to an ItemsSource of a TreeView. Each node will also have a child property that is another list of nodes.

Each of you nodes could just be a simple class, or could actually be ViewModels themselves if you need to put ICommands/Actions/Methods at each node.

Each node can be setup however you wish in C# in your ViewModel, since HierarchicalDataTemplate does alot of binding work for you.

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