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

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

问题描述

假设我有一个客户"窗口,其中显示有关客户的信息,例如姓名,地址和电话号码.在底部有一个订单的DataGrid.当然,客户具有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.

所以现在的问题是:如何将订单从客户"视图模型传递到订单"用户控件的视图模型?因为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...

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

I can't say ... because the user control has it's own view model.

我要说

由于这种气味,您遇到了这个问题,这应该表明您做错了事.

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中考虑用户控件-对于每个模型,您都有一个用户控件,它旨在向用户显示该模型中的数据.您可以在要向用户显示该模型的任何地方使用它.需要按钮吗?公开用户控件上的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天全站免登陆