CanExecute Logic for DelegateCommand

前端 未结 3 1298
名媛妹妹
名媛妹妹 2020-12-14 21:22

Update: The focus became MVVM instead of the actual question so I\'m updating it.

I\'m having a problem with CanExecute for Deleg

相关标签:
3条回答
  • 2020-12-14 21:36

    If you want to stick to DelegateCommand you can use ObservesCanExecute:

    DelegateSaveCommand = new DelegateCommand(Save, CanSaveDelegate).ObservesCanExecute(CanSaveDelegate);
    

    Note that there is also ObservesProperty available if you are using a property for your CanExecute check. But then your property has to call NotifyPropertyChanged.

    0 讨论(0)
  • 2020-12-14 21:39

    As it already was mentioned, this is intended behavior of DelagateCommand, not a bug. DelegateCommand doesn't raise CanExecuteChanged event automatically, you have to raise that event manually by calling RaiseCanExecuteChanged when appropriate. Whereas RelayCommand relays on CommandManager.RequerySuggested event for that. This event is raised every time the user clicks somewhere or presses a button.

    For situations when it is not very convenient or there is no appropriate place for calling RaiseCanExecuteChanged (like in your scenario you have to subscribe to PropertyChanged event on the model, etc) I have created the following simple wrapper that ensures that the CanExecute method of the wrapped command is executed automatically on CommandManager.RequerySuggested event:

    public class AutoCanExecuteCommandWrapper : ICommand
    {
        public ICommand WrappedCommand { get; private set; }
    
        public AutoCanExecuteCommandWrapper(ICommand wrappedCommand)
        {
            if (wrappedCommand == null) 
            {
                throw new ArgumentNullException("wrappedCommand");
            }
    
            WrappedCommand = wrappedCommand;
        }
    
        public void Execute(object parameter)
        {
            WrappedCommand.Execute(parameter);
        }
    
        public bool CanExecute(object parameter)
        {
            return WrappedCommand.CanExecute(parameter);
        }
    
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    }
    

    You can use it like this:

    DelegateSaveCommand = new AutoCanExecuteCommandWrapper(new DelegateCommand(Save, CanSaveDelegate));
    
    0 讨论(0)
  • 2020-12-14 22:00

    There is a bug in the DelegateCommand provided by Prism which doesn't raise the CanExecute event. I beat my head against the wall for a day until I dove into the DelegateCommand class provided by the Prism framework. I don't have the code with me, but I can post my resolution in a bit.

    The alternative is to use one of the other RelayCommand frameworks out there.

    Edit
    Rather than reposting the code, there are other SO questions that provide resolutions:

    • WPF-Prism CanExecute method not being called

    And Kent B. has a good article: MVVM Infrastructure: DelegateCommand

    0 讨论(0)
提交回复
热议问题