Button doesn't become disabled when command CanExecute is false

后端 未结 2 1114
闹比i
闹比i 2021-01-04 20:29

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

相关标签:
2条回答
  • 2021-01-04 21:07

    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.

    0 讨论(0)
  • 2021-01-04 21:22

    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题