使用MVVM模式在WPF中使用命令

时间:2022-10-22 11:24:44

I am faced with a problem that might be known by other developers and I am trying to figure it out and understand it well. It doesn't have to be directly connected to MVVM/WPF, it can also be connected to other methodologies like MVC and MVP .

我面临着其他开发人员可能已知的问题,我正在努力弄清楚并理解它。它不必直接连接到MVVM / WPF,它也可以连接到其他方法,如MVC和MVP。

Lets assume I have a shop cart Model that has its own Service, addItem and deleteItem Methods and calculates the price of the shop cart etc.

让我们假设我有一个购物车模型,它有自己的服务,addItem和deleteItem方法,并计算购物车的价格等。

Now in my View I display this shop cart and when I want to show the total sum of the shop cart I click on a button to trigger the method that take cares of this.

现在,在我的视图中,我显示了这个商店购物车,当我想显示商店购物车的总金额时,我点击一个按钮来触发照顾这个的方法。

Here comes the problem: How can I bind the Method (or Command) to the Button? I know that I have to use ICommand, but by using this interface I kind of break the rules of responsibility separation. How can I implement, without break the rules of the MVVM pattern.

问题出现了:如何将方法(或命令)绑定到Button?我知道我必须使用ICommand,但是通过使用这个界面,我打破了责任分离的规则。如何在不违反MVVM模式规则的情况下实现。

使用MVVM模式在WPF中使用命令

2 个解决方案

#1


4  

What you call PresentationModel, is the ViewModel in WPF, which is the DataContext of the View.
This ViewModel holds an instance of ShopCart. It also holds the commands, so that the view can bind to them.
The command, lets name it CalculatePrice should just invoke the calculatePrice() method of the ShopCart.

你称之为PresentationModel的是WPF中的ViewModel,它是View的DataContext。此ViewModel包含ShopCart的实例。它还包含命令,以便视图可以绑定到它们。命令,让它命名为CalculatePrice应该只调用ShopCart的calculatePrice()方法。

The question is, how do you define it that way:
Well, I like using RelayCommand, which allows you to define commands using lambda expressions.

问题是,你如何定义它:嗯,我喜欢使用RelayCommand,它允许你使用lambda表达式定义命令。

So then you can have a property public RelayCommand CalculatePrice, which you define in the constructor:

那么你可以拥有一个属性public RelayCommand CalculatePrice,你可以在构造函数中定义它:

public ViewModel(){
    CalculatePrice = new RelayCommand(param => this.ShopCart.calculatePrice());
}

That way, you can bind the CalculatePrice command to the button, which then executes ShopCart.calculatePrice().

这样,您可以将CalculatePrice命令绑定到按钮,然后按钮执行ShopCart.calculatePrice()。

#2


2  

If I understood correctly, in your view you display a set of ShopCarts, and you want to be able to call the ShopCart.calculatePrice() method. I would say, what you need to do, is to store the ICommand object in your ViewModel, and bind the ShopCart to the CommandParameter property. This way, you will receive the ShopCart as the parameter of the CanExecute and Execute methods.

如果我理解正确,在您的视图中显示一组ShopCarts,并且您希望能够调用ShopCart.calculatePrice()方法。我想说,你需要做的是将ICommand对象存储在ViewModel中,并将ShopCart绑定到CommandParameter属性。这样,您将收到ShopCart作为CanExecute和Execute方法的参数。

#1


4  

What you call PresentationModel, is the ViewModel in WPF, which is the DataContext of the View.
This ViewModel holds an instance of ShopCart. It also holds the commands, so that the view can bind to them.
The command, lets name it CalculatePrice should just invoke the calculatePrice() method of the ShopCart.

你称之为PresentationModel的是WPF中的ViewModel,它是View的DataContext。此ViewModel包含ShopCart的实例。它还包含命令,以便视图可以绑定到它们。命令,让它命名为CalculatePrice应该只调用ShopCart的calculatePrice()方法。

The question is, how do you define it that way:
Well, I like using RelayCommand, which allows you to define commands using lambda expressions.

问题是,你如何定义它:嗯,我喜欢使用RelayCommand,它允许你使用lambda表达式定义命令。

So then you can have a property public RelayCommand CalculatePrice, which you define in the constructor:

那么你可以拥有一个属性public RelayCommand CalculatePrice,你可以在构造函数中定义它:

public ViewModel(){
    CalculatePrice = new RelayCommand(param => this.ShopCart.calculatePrice());
}

That way, you can bind the CalculatePrice command to the button, which then executes ShopCart.calculatePrice().

这样,您可以将CalculatePrice命令绑定到按钮,然后按钮执行ShopCart.calculatePrice()。

#2


2  

If I understood correctly, in your view you display a set of ShopCarts, and you want to be able to call the ShopCart.calculatePrice() method. I would say, what you need to do, is to store the ICommand object in your ViewModel, and bind the ShopCart to the CommandParameter property. This way, you will receive the ShopCart as the parameter of the CanExecute and Execute methods.

如果我理解正确,在您的视图中显示一组ShopCarts,并且您希望能够调用ShopCart.calculatePrice()方法。我想说,你需要做的是将ICommand对象存储在ViewModel中,并将ShopCart绑定到CommandParameter属性。这样,您将收到ShopCart作为CanExecute和Execute方法的参数。