Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Should commands in MVVM know of their viewmodel?

My question is basically the headline.

The background is that I made a small WPF program to control a motorized stage.
I have a view which show the stage position, stage status (IsMoving – true/false) and have a move button whose action is bound to a command (i.e. MoveCommand).
The MoveCommand CanExecute should return True/False if the stage is idle/moving respectively.

Right now I accomplished that by passing the ViewModel to the MoveCommand constructor – and this way the command can access the IsMoving property that the ViewModel expose, but I’m not sure if that is correct/best practice to achieve this.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is the current code code I’m using:
ViewModel:

class StageViewModel : INotifyPropertyChanged
{
     private bool _isMoving;
     public ICommand MoveStage { get; private set; }
     public StageViewModel()
     {
        MoveStage = new MoveStageCommand(this);
     }

     public bool IsMoving 
     {
        get { retrun _isMoving; }
        private set 
        {
           _isMoving = value;
           OnPropertyChanged(nameof(IsMoving));
        }
     }

     //event from the stage model updating if it is moving/idle.
     private void Model_StageStatusUpdate(bool isMoving)
     {
        IsMoving = isMoving;
     }
}

Code for the MoveCommand:

class MoveCommand : ICommand
{
    private StageViewModel _stageViewModel;
    public MoveCommand(StageViewModel stageViewModel)
    {
        _stageViewModel = stageViewModel;
    }
    public bool CanExecute(object? parameter)
    {
        return !(_stageViewModel.IsMoving);
    }
}

Thanks in advance for any suggestions.

>Solution :

Yes. If that command only makes sense in combination with that specific view model, that’s perfectly fine.

In fact, a common pattern in view models is to create the commands with a DelegateCommand helper class. DelegateCommand is not a built-in WPF type, but can be found in almost all MVVM libraries, and a large percentage of those not using a library have probably implemented the type themselves in one way or another.

It’s just a container for an Execute lambda (and possibly also a CanExecute lambda), which captures the required state directly from within the view model, and it allows you to declare short commands directly in the constructor of your view model, without having to litter your code with dozens of tiny command classes.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading