locked
Commands really needed to use ViewModel? RRS feed

  • Question

  • To put the code to respond to a button click or the selection of a menu item in a ViewModel class (away from the code-behind), it's normally recommended to use Commands. However, what would be wrong with assigning Click="<AMethodInTheCodeBehind>"

    It seems like this would be easier. Would it be a simple matter of doing this (assuming the DataContext is named "vm"):

    Click="vm.SomeMethod" ?


    Photographer/Writer/Drummer: www.capturewisconsin.com (vote now!)
    Wednesday, May 11, 2011 9:51 PM

Answers

  • Sure, you can react to some view event in view's code-behind and call view-model's method.

    This way you are doing 'manual binding' of user action to view-model's method.

    Commands are there to make it easier for you to do this from XAML only. And there are also some plumbing with CanExecuteChanged.

    So there is nothing wrong in this approach. It would be problematic if you would be passing views as arguments to view-model's methods. That would violate MVVM pattern.


    www.GemBoxSoftware.com/Ppc/Overview.htm Advanced adCenter and AdWords API for C# / VB.NET
    • Marked as answer by Min Zhu Thursday, May 19, 2011 2:41 AM
    Thursday, May 12, 2011 2:34 PM
  • Hi B Clay Shannon,

    Please see my reply in your other post http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/bac30c08-359a-412e-b8f6-1b60457ae3b4.

    Let me know if you have any questions.

    Best regarsd,


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Min Zhu Thursday, May 19, 2011 2:41 AM
    Thursday, May 12, 2011 6:11 AM
  • It seems that everyone has a misunderstanding of what commands are.  Commands are not there to separate the View and ViewModel, and they are not there to make it easier to do this from XAML.  You can easily use commands without using MVVM.

    One thing to realize is that a Command is NOT an event.  Commands have several purposes.  "The first purpose is to separate the semantics and the object that invokes a command from the logic that executes the command. This allows for multiple and disparate sources to invoke the same command logic, and it allows the command logic to be customized for different targets."  "Another purpose of commands is to indicate whether an action is available."  This is where the CanExecute comes into play.  Based on the result of the CanExecuteChange.CanExecute the element will be enabled if it is True or disabled if it is False.

    Other things can come into play with commands such as command sources, command bindings, command targets, as well as the command manager.

    In either case it is perfectly acceptable to simply handle the click event of the button and call a method on your VM.

    • Marked as answer by Min Zhu Thursday, May 19, 2011 2:41 AM
    Thursday, May 12, 2011 9:45 PM
  • Commands are not required. They are just an alternate route to get somewhere.

    I generally find Commands to generally be more more databinding friendly when it comes to tieing databound elements to your ViewModel. As opposed to Events

    Events imho are a lot quicker if you're using Visual studio.

    Although, if you're writing or stubbing out your ViewModel and then doing your design in Expression, I think it's easier to bind your UX to your VM with Commands.

     

    But it's not a requirement, it's simply a preference.

    Hope this helps.

    • Marked as answer by Min Zhu Thursday, May 19, 2011 2:41 AM
    Friday, May 13, 2011 4:16 AM

All replies

  • Commands allow you to seperate the view and the viewmodel.

    Thursday, May 12, 2011 6:05 AM
  • Hi B Clay Shannon,

    Please see my reply in your other post http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/bac30c08-359a-412e-b8f6-1b60457ae3b4.

    Let me know if you have any questions.

    Best regarsd,


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Min Zhu Thursday, May 19, 2011 2:41 AM
    Thursday, May 12, 2011 6:11 AM
  • "Commands allow you to seperate the view and the viewmodel."

    I'm aware of that, I'm just wondering why the View and ViewModel can't be separated by referencing event handler code in the ViewModel from the View?


    Photographer/Writer/Drummer: www.capturewisconsin.com (vote now!)
    Thursday, May 12, 2011 2:06 PM
  • Sure, you can react to some view event in view's code-behind and call view-model's method.

    This way you are doing 'manual binding' of user action to view-model's method.

    Commands are there to make it easier for you to do this from XAML only. And there are also some plumbing with CanExecuteChanged.

    So there is nothing wrong in this approach. It would be problematic if you would be passing views as arguments to view-model's methods. That would violate MVVM pattern.


    www.GemBoxSoftware.com/Ppc/Overview.htm Advanced adCenter and AdWords API for C# / VB.NET
    • Marked as answer by Min Zhu Thursday, May 19, 2011 2:41 AM
    Thursday, May 12, 2011 2:34 PM
  • It seems that everyone has a misunderstanding of what commands are.  Commands are not there to separate the View and ViewModel, and they are not there to make it easier to do this from XAML.  You can easily use commands without using MVVM.

    One thing to realize is that a Command is NOT an event.  Commands have several purposes.  "The first purpose is to separate the semantics and the object that invokes a command from the logic that executes the command. This allows for multiple and disparate sources to invoke the same command logic, and it allows the command logic to be customized for different targets."  "Another purpose of commands is to indicate whether an action is available."  This is where the CanExecute comes into play.  Based on the result of the CanExecuteChange.CanExecute the element will be enabled if it is True or disabled if it is False.

    Other things can come into play with commands such as command sources, command bindings, command targets, as well as the command manager.

    In either case it is perfectly acceptable to simply handle the click event of the button and call a method on your VM.

    • Marked as answer by Min Zhu Thursday, May 19, 2011 2:41 AM
    Thursday, May 12, 2011 9:45 PM
  • Commands are not required. They are just an alternate route to get somewhere.

    I generally find Commands to generally be more more databinding friendly when it comes to tieing databound elements to your ViewModel. As opposed to Events

    Events imho are a lot quicker if you're using Visual studio.

    Although, if you're writing or stubbing out your ViewModel and then doing your design in Expression, I think it's easier to bind your UX to your VM with Commands.

     

    But it's not a requirement, it's simply a preference.

    Hope this helps.

    • Marked as answer by Min Zhu Thursday, May 19, 2011 2:41 AM
    Friday, May 13, 2011 4:16 AM
  • Hi B Clay Shannon,

    Just checking in to see if the information was helpful. Please let us know if you would like further assistance.

    Have a great day!


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 16, 2011 2:47 AM