Button doesn't become disabled when command CanExecute is false

后端 未结 2 1124
闹比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: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;
        }
    }
    

提交回复
热议问题