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
In addition to Isak Savo's contribution, you might want to have a look at Brian Keating's blog for a working sample.
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:
/// <summary>
/// Contains helper methods for UI, so far just one for showing a waitcursor
/// </summary>
public static class UiServices
{
/// <summary>
/// A value indicating whether the UI is currently busy
/// </summary>
private static bool IsBusy;
/// <summary>
/// Sets the busystate as busy.
/// </summary>
public static void SetBusyState()
{
SetBusyState(true);
}
/// <summary>
/// Sets the busystate to busy or not busy.
/// </summary>
/// <param name="busy">if set to <c>true</c> the application is now busy.</param>
private static void SetBusyState(bool busy)
{
if (busy != IsBusy)
{
IsBusy = busy;
Mouse.OverrideCursor = busy ? Cursors.Wait : null;
if (IsBusy)
{
new DispatcherTimer(TimeSpan.FromSeconds(0), DispatcherPriority.ApplicationIdle, dispatcherTimer_Tick, Application.Current.Dispatcher);
}
}
}
/// <summary>
/// Handles the Tick event of the dispatcherTimer control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private static void dispatcherTimer_Tick(object sender, EventArgs e)
{
var dispatcherTimer = sender as DispatcherTimer;
if (dispatcherTimer != null)
{
SetBusyState(false);
dispatcherTimer.Stop();
}
}
}
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.
So I didn't like using OverrideCursor because I had multiple windows and I wanted the ones that were not currently executing something to have the normal arrow cursor.
Here is my solution:
<Window.Style>
<Style TargetType="Window">
<Style.Triggers>
<DataTrigger Binding="{Binding IsBusy}" Value="True">
<Setter Property="Cursor" Value="Wait" />
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Style>
<Grid>
<Grid.Style>
<Style TargetType="Grid">
<Style.Triggers>
<DataTrigger Binding="{Binding IsBusy}" Value="True">
<Setter Property="IsHitTestVisible" Value="False" /> <!-- Ensures wait cursor is active everywhere in the window -->
<Setter Property="IsEnabled" Value="False" /> <!-- Makes everything appear disabled -->
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Style>
<!-- Window controls go here -->
</Grid>