Brian Watt
Brian Watt

Reputation: 235

CanExecute methods not firing after moving to Catel 4.x

Following up on this question and its answer (which did solve my problem), I have a couple of questions:

  1. Where do I retrieve the ViewModelManager to pass to the constructor of RequeryAllTheThings? Currently, I am using the ViewModelBase.ViewModelManager property in the first view model that loads, but this seems inelegant. I tried using the ServiceLocator, but the object it returned never managed any view models.
  2. The answer implies that there is an approach that will perform better, but I have not been able to find it in the documentation or on SO. Specifically, I need the CanExecute methods to fire for my commands when a control in a DataGrid changes state. What is the correct approach?

Upvotes: 1

Views: 51

Answers (1)

Geert van Horrik
Geert van Horrik

Reputation: 5724

  1. The IViewModelManager is registered in the ServiceLocator. Use the TypeFactory (which will resolve dependencies automatically from the ServiceLocator) to construct your types that require the IViewModelFactory.

  2. The correct approach is to only invalidate the state of commands when it's necessary. The CommandManager.RequerySuggested invalidates nearly on any routed command (mouse moves, keyboards, etc). While this might work great on your Intel Core i7 super powered cpu, there are people in the world running slower machines. So Catel tries to find the best "out-of-the-box" experience possible, which is to invalidate the state if a property on the view model changes (which is normally the cause of a command state invalidation).

You could create your own watcher that listens to the relevant DG events also invalidate the commands in case of such an event.

Upvotes: 1

Related Questions