Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to intercept execution of a RoutedCommand within the view? (WPF / MVVM)

In a WPF / MVVM application, I am trying to find a code-efficient way to set the main view cursor to Cursors.Wait prior to any lengthy operation and to restore it to the default cursor after completion.

Since all operations are invoked using routed commands, I'd like to find a way of intercepting command execution, ideally by creating a generic command class that wraps an instance of a built-in routed command, but I can't visualize how to do this.

Specifically, the RoutedCommand.Execute method is not virtual so I need another mechanism to intercept its calls. Also, I am not sure how an instance of the generic command class would reference the view for which it has to set the cursor.

Any advice please?

like image 859
Tim Coulter Avatar asked Jul 07 '10 10:07

Tim Coulter


1 Answers

You can set the Cursor in a static manner. The effect is that the cursor will be 'Wait' while the app has focus rather than when it is over a certain control.

The code, which would be part of the ViewModel at the start of the lengthy operation:

Mouse.OverrideCursor = Cursors.Wait;

After the operation is complete you have to clear the override like this:

Move.OverrideCursor = null;
like image 94
Steven Avatar answered Oct 03 '22 19:10

Steven