I have a grid where every element have a button with command. When i click a button i set CanExecute to false and all buttons are disabled. How can i disable only one button that i click?
My command:
public RelayCommand SignDocumentsCommand
{
get
{
return signDocumentsCommand ??
(signDocumentsCommand = new RelayCommand(obj => MyMethod(), () => !IsEnabled));
}
}
My RelayCommand:
public class RelayCommand : ICommand
{
private readonly Action<object> execute;
private readonly Func<bool> canExecute;
public event EventHandler CanExecuteChanged
{
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}
public RelayCommand(Action<object> execute, Func<bool> canExecute = null)
{
this.execute = execute;
this.canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return canExecute == null || canExecute();
}
public void Execute(object parameter)
{
execute(parameter);
}
}
You have 2 options.
1) move the command to the objects that are in the grid, so that each of them has seperate copy of the command and its execution depends on the object properties
2) add "CanXXXX" properties to your objects and use Style.DataTrigger
to disable the button in each row
The second one will consume much less memory, but is less MVVMy