Search code examples
wpfmvvmuser-interfacerouted-commands

Is it possible to have CommandManager requery only specific WPF command instead of all?


I'm trying to implement a highly responsive UI for my MVVM application, so I've chosen to have all command handlers automatically execute in a BackgroundWorker so they don't block the UI.

But at the same time, I don't want the user to be able to execute the same command while it is still executing in the background. The solution seems obvious:

  1. When Executed handler is invoked, have the CanExecute handler return false
  2. Start the BackgroundWorker async
  3. When BackgroundWorker finishes, have the CanExecute handler return true again.

Problem is, I need to notify WPF after step 1 and again after step 3 that CanExecute has changed and that it should be required. I know I can do it by calling CommandManager.InvalidateRequerySuggested, but that causes CanExecute handlers of all other commands to be requeried as well. Having a lot of commands, that's not good.

Is there a way to ask for requery of a specific command - i.e. the one that is currently being executed?

TIA


Solution

  • I think you only need to raise the ICommand.CanExecuteChanged event for these two cases, and the corresponding control should update its IsEnabled state.