WPF MVVM command canexecute enable/disable button

后端 未结 4 835
悲哀的现实
悲哀的现实 2020-12-03 14:51

I want to enable RibbonButton when textbox property text isn\'t null. Disable RibbonButton when textbox property text is null. I want to use CanExecute method in ICommand fo

4条回答
  •  心在旅途
    2020-12-03 15:33

    Last time I used Microsoft.Practices.Prism.Commands namesapce from Microsoft.Practices.Prism.dll. Class DelegateCommand has own RaiseCanExecuteChanged() method. So the benifit is you don't have to write yout own implementation of ICommand.

    XAML:

    
        
        

    ViewModel:

    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;
        }
    }
    

提交回复
热议问题