带有 MVVM 和用户控件的 WPF 数据绑定 [英] WPF DataBinding with MVVM and User Controls

查看:22
本文介绍了带有 MVVM 和用户控件的 WPF 数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个客户窗口,显示有关客户的信息,例如姓名、地址和电话号码.在底部有他们订单的 DataGrid.当然 Customer 有一个 Orders 属性,所以如果你使用的是 MVVM,你只需设置:

Let's say I have a Customer Window showing information about a customer, like Name, address and phone number. On the bottom there is a DataGrid of their orders. Of course the Customer has an Orders property, so if you're using MVVM, you would just set:

ItemsSource = "{Binding Customer.Orders}"

但是,现在假设数据网格现在是用户控件的一部分,该控件还包括用于编辑/添加/删除订单的控件.我想在多个地方使用这组相同的控件,并且我希望将用于编辑/添加/删除 Order 对象的所有逻辑封装在用户控件中.并且因为我想使用命令而不是事件处理程序,所以我希望用户控件拥有自己的视图模型.

However, now let's say that the data grid is now part of a user control, which also includes controls for editing/adding/removing Orders. I want to use this same set of controls in multiple places, and I would like all the logic for editing/adding/removing Order objects to be encapsulated in the user control. And because I want to use commands, rather than event handlers, I would like the user control to have its own view model.

那么现在的问题是:如何将订单从 Customer 视图模型传递到 Orders 用户控件的视图模型?因为 Orders 用户控件会绑定到一个视图模型,所以我不能说:

So now the question is: how do I pass the orders from the Customer view model to the Orders user control's view model? Because the Orders user control will be bound to a view model, I can't say:

<local:OrdersUserControl DataContext="{Binding Customer.Orders}" />

因为用户控件有它自己的视图模型.它希望在那里看到 Customer.Orders,当然不是.

because the user control has it's own view model. It would expect to see Customer.Orders there, and of course it's not.

我想这是先有鸡还是先有蛋的情况.

I guess this is kind of a chicken or the egg situation.

感谢您的帮助.

亚伦

推荐答案

现在我的每周不要那样做"回答...

And now for my weekly "don't do that" answer...

我不能说......因为用户控件有它自己的视图模型.

我要说的

您遇到此问题是因为那种气味,这应该表明您做错了什么.

You're experiencing this issue because of that smell, and it should be an indication that you're doing something wrong.

解决方案是放弃为 UserControl 构建的 VM.如果它包含业务逻辑,则应将其移至另一个 ViewModel 中的适当位置.

The solution is to ditch the VM built for the UserControl. If it contains business logic, it should be moved to an appropriate location in another ViewModel.

您应该认为 UserControl 只不过是一个更复杂的控件.TextBox 是否有自己的 ViewModel?不.您将您的 VM 的属性绑定到控件的Text 属性,然后控件会在其 UI 中显示您的文本.

You should think of a UserControl as nothing more than a more complex control. Does the TextBox have its own ViewModel? No. You bind your VM's property to the Text property of the control, and the control shows your text in its UI.

MVVM 并不意味着没有代码隐藏.将用户控件的 UI 逻辑放在代码隐藏中.如果它非常复杂,以至于您需要在用户控件中使用业务逻辑,则表明它太包罗万象了.将其分解为两个或更多.

MVVM doesn't mean no codebehind. Put your UI logic for your user control in the codebehind. If it is so complex that you need business logic inside the user control, that suggests it is too encompassing. Break it down into two or more.

像这样考虑 MVVM 中的 UserControl--对于每个模型,您都有一个 UserControl,它旨在向用户展示该模型中的数据.您可以在任何想要向用户显示该模型的地方使用它.它需要一个按钮吗?在您的 UserControl 上公开一个 ICommand 属性,并让您的业务逻辑绑定到它.您的业​​务逻辑是否需要了解内部发生的事情?添加路由事件.

Think of UserControls in MVVM like this--For each model, you have a UserControl, and it is designed to present the data in that model to the user. You can use it anywhere you want to show the user that model. Does it need a button? Expose an ICommand property on your UserControl and let your business logic bind to it. Does your business logic need to know something going on inside? Add a routed event.

通常,在 WPF 中,如果您发现自己问为什么做某事会很痛,那是因为您不应该这样做.

Normally, in WPF, if you find yourself asking why it hurts to do something, it's because you shouldn't do it.

这篇关于带有 MVVM 和用户控件的 WPF 数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆