mrousavy / Jellyfish

🐟 An incredibly lightweight and type safe MVVM library for .NET WPF, Silverlight, Xamarin and UWP
MIT License
21 stars 4 forks source link

Re-eval CanExecute on RelayCommand #2

Closed mrousavy closed 6 years ago

mrousavy commented 6 years ago

Automatically re-evaluate the RelayCommand::CanExecute(T t) function whenever the parameter t changes.

Before:

ICommand LoginCommand = new RelayCommand<MyObject>(LoginAction, CanExecute);
// ...
bool CanExecute(MyObject parameter)
{
    return parameter.SomeValue == true;
}
// ...
public T MyObject
{
    get => _myObject;
    set
    {
        Set(ref _myObject, value);
        LoginCommand.RaiseCanExecuteChanged(); // re-evaluate CanExecute(T)
    }
}

After:

ICommand LoginCommand = new RelayCommand<MyObject>(LoginAction, CanExecute);
// ...
bool CanExecute(MyObject parameter)
{
    return parameter.SomeValue == true;
}

Possible approaches:

mrousavy commented 6 years ago
public event EventHandler CanExecuteChanged
{
    add => CommandManager.RequerySuggested += value;
    remove => CommandManager.RequerySuggested -= value;
}

??

mrousavy commented 6 years ago

c3951e4 fixed it.