Search code examples
wpfmvvmmvvm-lightcommand-pattern

WPF: MVVM: Command vs CallMethodAction?


I'm learning the MVVM pattern with a new(small) project, and I've one question about the way to invoke actions on our controller:

I saw many tutorial where they were telling us to use Command, implying to declare a RelayCommand, initialize it and create the action called by the RelayCommand.

In the other side, I've a colleague which said me that I can use the CallMethodAction with a trigger:

<i:Interaction.Triggers> 
  <i:EventTrigger> 
    <ei:CallMethodAction MethodName="Init" TargetObject="{Binding}" /> 
  </i:EventTrigger> 
</i:Interaction.Triggers> 

For me, his approach has the advantage that I don't have to make some inits methods for commands(which may be never used).

So what am I missing? Why everybody use commands?


Solution

  • Commands provide functionality for disabling in ViewModel code. That can be used to automatically disable e.g buttons bound to command. That's what makes Commands better. Besides, based on your logic you could just dynamically pluck another Command in the same slot and it will reroute the traffic from View, whereas in CallMethodAction you would have to write the rerouting logic in the called method, which would be ugly.

    As you can see, it depends on what you try to accomplish and how complicated is your logic :)