I'm migrating a tool from MVVM Light 4.0.3 to 5.4.1 and I have found a very odd issue with the newest RelayCommand implementation.
This is the old implementation in the V4.0.3:
This is the newest implementation in the V5.4.1:
Before I was able to use variables to define the canExecute behaviour (enabled a button) with the following code:
public ICommand GetNewItemsFromDB { get; private set; }
private bool _IsActive;
public bool IsActive
{
get
{
return _IsActive;
}
set
{
if (_IsActive != value)
{
_IsActive = value;
this.RaisePropertyChanged(() => IsActive);
}
}
}
GetNewItemsFromDB = new RelayCommand(GetDataFromDB, () => { return IsActive == false; });
private void GetDataFromDB()
{
IsActive = true;
}
The previous code was able to enable the button without any issue in the MVVM Light 4.0.3; however, in the newest implementation is always disabled, I added changed a bit since there is a new definition of keepTargetAlive:
GetNewItemsFromDB = new RelayCommand(GetDataFromDB, () => { return IsActive == false; }, true);
Also, I tried the false option and nothing changed. The only way that I found to re-enabled it, it was to set a predefined value like this one:
GetNewItemsFromDB = new RelayCommand(GetDataFromDB, () => true, true);
This implementation is going to be useless in my case since the RelayCommand (in namespace GalaSoft.MvvmLight.Command) depends on the variable IsActive, which determines if it's enabled or not. Does anyone what I should change in the V5 to make it work? Thanks for your suggestions.
If I am understanding this correctly.
If you are using this class in WPF4.5 or above, you need to use the
GalaSoft.MvvmLight.CommandWpf
namespace (instead ofGalaSoft.MvvmLight.Command
). This will enable (or restore) the CommandManager class which handles automatic enabling/disabling of controls based on the CanExecute delegate.
And in the release notes:
Important note about issue 7659: In order to fix the issue where the controls are not disabled anymore depending on the state of the RelayCommand.CanExecute delegate, you need to make a small change into your code. To opt-in into the fixed behavior, please change the namespace you are using from
GalaSoft.MvvmLight.Command
toGalaSoft.MvvmLight.CommandWpf
.
I remember correctly, somewhere in ancient history I had to do this myself for a project.