MvvmLight and CanExecute

Today is going to be a very short post, more of a pingback really.

We are using MvvmLight as it is the defacto mvvm library of WPF but when I upgraded the libraries from the .Net 4.0 versions to .Net 4.6.1 the other day most of our application broke down. The CanExecute callback of all our relay commands stopped working (and I would’ve know this far earlier if the CI had been working, lesson learned again).

After a whole lot of digging I came across this comment in the source code:

If you are using this class in WPF4.5 or above, you need to use the GalaSoft.MvvmLight.CommandWpf namespace (instead of GalaSoft.MvvmLight.Command). This will enable (or restore) the CommandManager class which handles automatic enabling/disabling of controls based on the CanExecute delegate.

Which can be tracked back to the release notes of MvvmLight version 5.0.2:

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 to GalaSoft.MvvmLight.CommandWpf

So, in order to get this working again, make sure you reference the RelayCommand in GalaSoft.MvvmLight.CommandWpf instead.

using GalaSoft.MvvmLight.CommandWpf;

That’s all for today folks. Until next time