The ICommand
interface also defines a CanExecute
method. It's possible for you to make that command return false
when the execution starts, and set it back to true
when it's done. This also gives you the benefit of having the button disabled during the command's execution.
I don't work with RelayCommand
, so I'm not sure if it has an equivalent to DelegateCommand
's RaiseCanExecuteChanged
method, but using DelegateCommand
(which essentially does the same thing as RelayCommand
), you could do something like this (note that this implementation isn't thread safe):
SaveCommand = new DelegateCommand<CommandParam>(MyCommandExecute, MyCommandCanExecute);
private bool canExecute;
private bool MyCommandCanExecute()
{
return canExecute;
}
private void MyCommandExecute(CommandParam parm)
{
// Change the "can execute" status and inform the UI.
canExecute = false;
SaveCommand.RaiseCanExecuteChanged();
DoStuff();
// Change the "can execute" status and inform the UI.
canExecute = true;
SaveCommand.RaiseCanExecuteChanged();
}