1

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 .

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.

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.

Example:

3
  • you will implement it in ViewModel of the VIew Commented Jan 26, 2016 at 13:40
  • 1
    Look at implementation of ICommand msdn.microsoft.com/en-us/library/… and stackoverflow.com/questions/1135983/… Commented Jan 26, 2016 at 13:50
  • Using ICommand in ViewModel is not a violation of MVVM pattern, as commands are a presentation concern and hence belong to the ViewModel. @Domysee pointed it already out, in the command you just relay it to the service layer. Remember, that you may rise a notification or set the new price to a property that does rise the notification it its setter Commented Jan 26, 2016 at 15:56

2 Answers 2

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.

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

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

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().

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.