I have a simple-as-can be window with a button tied to a ViewModel with a command.
I expect the button to be disabled if MyCommand.CanExecute() is false. But it seem
I see you're using Prism and its NotificationObject
and DelegateCommand
, so we should expect there not to be a bug in RaiseCanExecuteChanged().
However, the reason for the behaviour is that Prism's RaiseCanExecuteChanged operates synchronously, so CanDoStuff()
is called while we're still inside the implementation of ICommand.Execute()
and the result then appears to be ignored.
If you create another button with its own command and call _myCommand.RaiseCanExecuteChanged()
from that command/button, the first button will be enabled/disabled as you expect.
Or, if you try the same thing with MVVM Light and RelayCommand your code will work because MVVM Light's RaiseCanExecuteChanged
calls CommandManager.InvalidateRequerySuggested()
which invokes the callback to CanDoStuff
asynchronously using Dispatcher.CurrentDispatcher.BeginInvoke
, avoiding the behaviour you're seeing with Prism's implementation.
You can try this (Microsoft.Practices.Prism.dll
is necessary)
public class ViewModel
{
public DelegateCommand ExportCommand { get; }
public ViewModel()
{
ExportCommand = new DelegateCommand(Export, CanDoExptor);
}
private void Export()
{
//logic
}
private bool _isCanDoExportChecked;
public bool IsCanDoExportChecked
{
get { return _isCanDoExportChecked; }
set
{
if (_isCanDoExportChecked == value) return;
_isCanDoExportChecked = value;
ExportCommand.RaiseCanExecuteChanged();
}
}
private bool CanDoExptor()
{
return IsCanDoExportChecked;
}
}