Although I have found several answers to this question I somehow don’t get it. So please excuse me asking.
I have a WPF application following the MVVM pattern. It contains a button which is bound to a command in the view model:
<button Content="Login" Command="{Binding ProjectLoginCommand}"/>
The commands are using RelayCommand. Now I would like to do the following:
- The user clicks the button and the corresponding command is executed. This works.
- Within this command another button shall be deactivated, i.e. it shall not be clickable.
I found that this should be possible using CanExecute but being honest: I simply don’t get it. Hoe can I set the button to enabled/disabled?
This is the RelayCommand.cs:
namespace MyApp.Helpers {
class RelayCommand : ICommand {
readonly Action<object> execute;
readonly Predicate<object> canExecute;
public RelayCommand(Action<object> execute) : this(execute, null) {
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
this.execute = execute;
this.canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return canExecute == null ? true : canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
execute(parameter);
}
}
}
This is how I call the command:
RelayCommand getProjectListCommand;
public ICommand GetProjectListCommand {
get {
if (getProjectListCommand == null) {
getProjectListCommand = new RelayCommand(param => this.ProjectLogin());
}
return getProjectListCommand;
}
}
When you use the RelayCommand you can specify two methods. The first method is the main method you want to run when the command is invoked. The second method you use to add in checks such as validation, and this should return a bool. if it returns false, then the main method will not run.
How it affects the button you have the command bound to, is it will run the boolean method continually, and while it return false, then the button the command is bound to will be disabled.
So in your command property:
Add in new method:
you can see how the CanExecute works if you put a break point in the bool method.