[forms-devel] Commanding: before executing command, always check CanExecute
Brian Lagunas
brian.lagunas at live.com
Tue May 31 22:57:46 UTC 2016
When dealing with MVVM and Commanding, any element that currently has a
Command property (TextCell, MenuItem, Button), do not check the
ICommand.CanExecute before invoking the command. Instead, currently it
seems to rely on the state of the UI, which is controlled by the
ICommand.CanExecute result.
The problem with this logic is that the result of the ICommand.CanExecute
can change before the UI is updated (assuming the developer even remembers
to update the UI by calling ChangeCanExecute) which will result in a Command
being invoked unexpectedly and bypassing any business logic that controls
the result of ICommand.CanExecute. The biggest violator of this is the
Button control and trying to prevent multiple clicks from a ViewModel.
While checking the CanExecute for the purpose of managing UI state is
important, we must also check the result of the ICommand.CanExecute before
actually invoking the command.
I have a PR waiting to submit that modifies TextCell.cs (line 84),
MenuItem.cs (line 76), and Button.cs (line 118) to include a check for
ICommand.CanExecute:
ICommand cmd = Command;
if (cmd != null && cmd.CanExecute(CommandParameter))
cmd.Execute(CommandParameter);
Is this a change that the team would consider adding?
Brian Lagunas
Senior Product Owner | Infragistics, Inc
<http://www.brianlagunas.com/> www.brianlagunas.com | @brianlagunas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/forms-devel/attachments/20160531/f57509cb/attachment.html>
More information about the forms-devel
mailing list