MVVM - 一个工作区中的几个工作区似乎没有意义

发布于 2024-08-31 06:03:41 字数 1369 浏览 6 评论 0原文

那么如何显示模型之间存在关系的复杂聚合ViewModel呢?

没有 wpf 弟子谈论过这一点,猜猜为什么它不可能......

你认为这是真的吗?

不明白我的意思吗?

看:CustomerViewModel 有许多OrderViewModel 和许多ProductViewModel

您有3个工作区来为所有3个ViewModels输入新数据,并且您有3个列表框/组合/数据网格来多选=>类型的集合; customerVM、orderVM 和 ProductVM。

UI 对于用户来说是有意义的,他不需要在添加每个新客户/订单/产品时关闭/打开工作区,这是一种糟糕的用户体验,您如何满足这种典型的 LOB 应用程序要求?

编辑:

您在 MVVM 中有一个 WorkSpace 类型的集合。 每个 ViewModel 都可以是一个工作区,因为它派生自 WorkSpace 类。 Workspaces 集合绑定到 ItemsSource afair josh smith 示例。

声明:我想使用成熟的 Windows 窗体用户界面,没有可关闭的视图模型... 问题:MVVM 只能使用工作区(可关闭并不重要...),因为 CustomerviewModel 必须已实例化,然后添加到绑定到 itemsControl 的工作区集合中,并根据数据类型进行数据模板化。

如果我没有保存 ViewModel 实例的工作区,我就无法对它们进行数据模板化,因为 MVVM 中的 ViewModel 将模型作为构造函数参数。 使用 CustomerViewModel 的数据类型对 UserControl 进行数据模板化将在 XAML 中引发异常!

现在看看我想要的 UI:我没有工作区,但 3 个 ViewModels = 3 个 UserControls datatemplated 意味着 3 次大爆炸……

你现在明白为什么我不喜欢 MVVM 了吗?它没有经过深思熟虑,我正在寻找解决方案...

当然,我可以将我的 ViewModels 又名 DataFormulars 放在 ObservableCollection 中,但我不希望它们绑定到 ItemsControl。我希望这 3 个 UserControl 在我的布局中有一定的位置。使用 ItemsControl 时,您会得到一个愚蠢的队列,如果新的 Dataformular 不适合现有空间,则会在该队列中添加并包装它。这就是垃圾布局设计/糟糕的用户界面。

现在你懂我了吗?

So how do you display complex aggregated ViewModels whose Models have relations to each other?

NO wpf disciple ever spoke about that, guess why its not possible...

Do you think thats true?

Don`t understand me?

Look: A CustomerViewModel has many OrderViewModel and those many ProductViewModel.

You have 3 Workspaces to enter the new data for all 3 ViewModels AND you have 3 listboxes/combobo/datagrid to multiselect Collections of type => customerVMs, orderVMs and productVM`s.

That the UI makes sense to the user he should not need to turn off/on the workspace every new customer/order/product is added what is a bad user experience, how do you do this typical LOB application requirement?

EDIT:

You have in MVVM a Collection of type WorkSpace.
Every ViewModel can be a Workspace because it derives from WorkSpace class.
The Workspaces collection is bound to a ItemsSource afair josh smith example.

Statement: I want to use goold mature windows forms user interfaces no closable viewmodels...
Problem: MVVM can only work with workspaces (closable doesnt matter...) because a CustomerviewModel must already be instantiated and then added to the workspaces collection bound to the itemsControl and datatemplated depending on the datatype.

If I would have no Workspaces which hold my ViewModel instances I could not datatemplate them, because a ViewModel in MVVM takes a model as constructor parameter.
DataTemplating a UserControl with DataType of the CustomerViewModel will throw a Exception in XAML!

Now look at my UI I want to have: I have no workspaces but 3 ViewModels = 3 UserControls datatemplated means 3 times a big bang...

You get now why I do not like MVVM? Its not well thought and I search for a solution...

Of course I could put my ViewModels aka DataFormulars in a ObservableCollection but I do not want them to be bound to an ItemsControl. I want that these 3 UserControl have a certain position in my layout. with an ItemsControl you have a stupid queue where a new Dataformular is just added and wrapped if it doesnt fit into the existing space. Thats is all rubbish layout design/bad UI.

You get me now?

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

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

发布评论

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

评论(1

时光磨忆 2024-09-07 06:03:41

您可以轻松地在一个窗口中拥有多个控件,每个控件绑定到不同的 ViewModel。为了在虚拟机之间进行协调,典型的方法是使用消息传递将不同的组件松散地耦合在一起。例如,您的 Customer ViewModel 可能会发送一条消息,说明您已选择一个订单,然后您的 OrderViewModel(可能在主窗口的一部分中可视化为控件)将看到该消息并更改显示的订单。

您可以使用共享服务类来完成类似的任务,该服务类公开您注入到每个虚拟机中的接口。然后,您可以绑定此共享接口的属性以允许虚拟机进行通信,这与消息的通信方式非常相似。我使用与此类似的东西来表示“SelectedObject”概念,其中可以选择各种不同的对象类型,并且我的应用程序中的不同虚拟机可能会以不同的方式对其进行可视化。例如,使用您的示例,如果产品成为 SelectedObject,您可能有一个“详细信息”面板,现在使用某些标准 ProductDataTemplate 呈现产品,并且另一个面板(显示订单)可能会更改产品列表上的 SelectedItem。您可以轻松拥有多个“工作区”,并且借助虚拟机用于协作的属性或消息集,所有工作区都保持彼此同步。

You can easily have multiple controls in a Window, each bound to a different ViewModel. To coordinate between the VMs, a typical approach is to loosely couple the different components together using message passing. For instance, your Customer ViewModel may send a message saying that you've selected an Order and then your OrderViewModel (visualized as a control in part of your main window, perhaps) will see the message and change the displayed Order.

You can accomplish something similar with a shared service class that exposes an interface which you inject into each of your VMs. You can then bind the properties of this shared interface to allow the VMs to communicate, much the same way as with messages. I've used something similar to this to represent a "SelectedObject" concept, where a wide range of different object types could be selected and different VMs in my app have different ways they might like to visualize that. For instance, using your example, if a Product became the SelectedObject, you might have a "detail" panel that now renders the Product using some standard ProductDataTemplate and perhaps another panel (displaying the Order) would change the SelectedItem on a Products list. You can easily have multiple 'workspaces' and all of them stay in sync with eachother by virtue of the set of properties or messages that the VMs use to collaborate.

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