gpt4 book ai didi

wpf - MVVM 中 ViewModels 之间耦合多少合适

转载 作者:行者123 更新时间:2023-12-01 05:42:51 24 4
gpt4 key购买 nike

我正在开发一个相当简单的 WPF 应用程序来显示对象层次结构以及所选对象的详细信息,带有 UserControl包装 TreeView在左 Pane (树控件)中,还有另一个 UserControl包装 ListView/GridView在右侧(详细信息控件)。

树控件使用 MVVM 跟随 this Josh Smith article相当接近,并且有许多 ViewModel 类型都派生自相同的基类型,TreeViewModel .使用 MainWindowViewModel 设置主窗口如 this Josh Smith article并暴露了 TreeViewModel用于填充第一代树控件。

但是,当我想填充右侧的详细信息 Pane 时,我遇到的问题是 SelectedItem树控件派生自 TreeViewModel当我需要一个完全不同类型的 ViewModel 用于详细信息 Pane 时,它将使用反射将对象扩展为属性/值表。

所以,问题:

  • 是否适合MainWindowViewModel揭露TreeViewModel对于树控件?我相信这里的答案是肯定的,但我愿意接受相反的建议。
  • 树控件中的选定项应如何适应详细信息 Pane 的正确 ViewModel 类型?一种选择似乎是 MainWindowViewModel跟踪树中选定的项目并进行调整,将其作为另一个属性公开,但我不确定是否有更好的解决方案。

  • 我是 WPF 和 MVVM 模式的新手,所以请原谅这个问题相当基本的性质。我已经对模式的背景进行了大量阅读,查看了一些示例应用程序等,但我找不到任何足够具体的内容来让我对答案充满信心。我也意识到 MVVM 对于这么简单的应用程序来说可能有点矫枉过正,但我​​将其部分用作学习练习。

    最佳答案

    1.Is it appropriate for the MainWindowViewModel to expose the TreeViewModel for the tree control?



    我相信是的。模型应该从逻辑上隐藏外观,但它不能隐藏像逻辑结构这样的东西。

    2.How should the selected item in the tree control be adapted to the right ViewModel type for the details pane? One option seems to be that the MainWindowViewModel tracks the selected item in the tree and does the adaption, exposing it as another property, but I'm not sure if there is a better solution.



    恕我直言不是。

    关于wpf - MVVM 中 ViewModels 之间耦合多少合适,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4254931/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com