I have a WPF application using the MVVM pattern that sometimes have to show a waitcursor when it is busy doing something the user has to wait for. Thanks to a combination of answers on this page: display Hourglass when application is busy, I have a solution that almost works (although it is not really MVVM in spirit).
Whenever I do something time-consuming in my viewmodels I do this:

(ShowWaitCursor() returns a IDisposable that shows the waitcursor until it is being disposed of)
The last line in my example is where I set some property. This property is bound in my XAML, e.g. like this:

<ItemsControl ItemsSource="{Binding SomeData}" />

However, since this could be a long list of objects and sometimes with complex datatemplates, etc. the actual binding and rendering sometime takes a considerable amount of time. Since this binding takes places outside of my using statement the waitcursor will go away before the actual wait is over for the user.

So my question is how to do a waitcursor in a WPF MVVM application that takes databinding into account?

5 Answers
5

Isak's answer did not work for me, because it did not solve the problem of how to act when the actual wait is over for the user.
I ended up doing this: Everytime I start doing something timeconsuming, I call a helper-method. This helper method changes the cursor and then creates a DispatcherTimer that will be called when the application is idle. When it is called it sets the mousecursor back:

Great implementation! This works perfect for me when the time consumption is happening in a third-party UI binding operation; when I don't really know when it is done. Thank you!
–
Sheldon WarkentinNov 2 '11 at 15:38

Jeff - nothing relinquishes it here. The cursor is reset when the application becomes idle. If ten lines of code indicate that the wait cursor should be displayed before the app becomes idle, it doesn't matter; they will all have finished by the time it is reset (obviously this solution assumes synchronous UI thread operations).
–
Stephen DrewSep 14 '12 at 13:39

1

@Steve: Of course! I didn't even see the dispatcher priority. Makes sense. Nice. I've used this in my code but I changed the class to WaitCursor and SetBusyState to Show so that my code is a little more self-documenting: WaitCursor.Show().
–
Jeff YatesSep 21 '12 at 21:50

1

You can't really use this directly with MVVM, since UiServices is not a static class and so it can't implement an interface. This causes a problem when trying to unit test the ViewModel. However I managed to work around it by renaming the class UiServicesStatic and nesting it inside a class called UiServices which implemented the IUiServices class. This interface has one method - void SetBusyState();
–
openshacJan 28 '14 at 14:27

What I've done in the past is to define boolean properties in the viewmodel that indicates that a lengthy calculation is in progress. For instance IsBusy which is set to true when working and false when idle.

Then in the view I bind to this and display a progress bar or spinner or similar while this property is true. I've personally never set the cursor using this approach but I don't see why it wouldn't be possible.

If you want even more control and a simple boolean isn't enough, you can use the VisualStateManager which you drive from your viewmodel. With this approach you can in detail specify how the UI should look depending on the state of the viewmodel.