How to show a waitcursor when the WPF application is busy databinding
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. Than开发者_运维技巧ks 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:
using (UiServices.ShowWaitCursor())
{
.. do time-consuming logic
this.SomeData = somedata;
}
(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?
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();
}
}
}
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>
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.
In addition to Isak Savo's contribution, you might want to have a look at Brian Keating's blog for a working sample.
精彩评论