MVVM Wait Cursor how to set the.wait cursor during invocation of a command?

wpf wait cursor
cursor current in wpf
wpf busy indicator
wpf loading overlay
mouse overridecursor

Scenario: User clicks a button on the View This invokes a command on the ViewModel, DoProcessing How, and where does the Wait cursor get set, considering the responsibilitues of View and ViewModel?

Just to be clear, I am just looking to change the DEFAULT cursor to an hourglass while the command is running. When the command completes, the cursor mut change back to an arrow. (It is a synchronous operation I am looking for, and I want the UI to block).

I have created an IsBusy property on the ViewModel. How do I ensure that the Application's mouse pointer changes?

I am using it successfully in my application:

/// <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, System.Windows.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();
        }
    }
}

This has been taken from here. Courtsey huttelihut.

You need to call the SetBusyState method every time you think you are going to perform any time consuming operation. e.g.

...
UIServices.SetBusyState();
DoProcessing();
...

This will automatically change your cursor to wait cursor when the application is busy and back to normal when idle.

Cursors.WaitCursor Property (System.Windows.Forms), wait cursor during invocation of a command? wpf wait cursor cursor current in wpf wpf cursor wpf busy indicator wpf loading overlay mouse overridecursor. Cursor. The Cursor that represents the wait cursor. Remarks. To force display of the wait cursor over a given control, call that control's UseWaitCursor method. To make the wait cursor display for the entire application, regardless of the control or form selected, call UseWaitCursor on the Application class. Applies to See also. Cursor

A very simple method is to simply bind to the 'Cursor' property of the window (or any other control). For example:

XAML:

<Window
    x:Class="Example.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     Cursor="{Binding Cursor}" />

ViewModel Cursor Property (Using Apex.MVVM):

    private NotifyingProperty cursor = new NotifyingProperty("Cursor", typeof(System.Windows.Input.Cursor), System.Windows.Input.Cursors.Arrow);
    public System.Windows.Input.Cursor Cursor
    {
        get { return (System.Windows.Input.Cursor)GetValue(cursor); }
        set { SetValue(cursor, value); }
    }

Then simply change the cursor in your view when needed...

    public void DoSomethingLongCommand()
    {
        Cursor = System.Windows.Input.Cursors.Wait;

        ... some long process ...

        Cursor = System.Windows.Input.Cursors.Arrow;
    }

MVVM, Gets the wait cursor, typically an hourglass shape. Cursors.WaitCursor Property. Definition. Namespace: System.Windows.Forms. Assembly: In this article. public static System.Windows.Input.Cursor Wait { get; } member this.Wait : System.Windows.Input.Cursor Public Shared ReadOnly Property Wait As Cursor Property Value Cursor. A wait cursor. Remarks. This Cursor typically indicates that the application is busy performing an operation. XAML Text Usage. See Remark on Cursors. Applies to

Command is handled on the view model, so the reasonable decission would be to do folowing:

1) Create a busy indicator service and inject it into the view model (this will allow you to replace the cursor logic with some nasty animation easily)

2) In the command handler call the busy indicator service to notify the user

I might be wrong, but it looks like you are trying to do some heavy calculations or I/O on UI thread. I highly recommend you to perform work on thread pool in this case. You can use Task and TaskFactory to easily wrap work with ThreadPool

Wpf Cursor Xaml, example, the click of a button, the movement of the mouse and many more. This interface is used a lot in MVVM applications, but it is useful not only in not be called while the Web client is waiting for a response from the Web server. is set to Transparent (and not to No Brush), the command will be invoked when the​  MVVM Wait curseur how to set the.curseur d'attente lors de l'invocation d'une commande? Scénario: L'utilisateur clique sur un bouton sur la Vue Ceci appelle une commande sur le Modèlevue, Comment, et où le curseur D'attente se positionne-t-il, compte tenu des responsabilités de View et ViewModel?

There is a great Session(at 50:58) by Laurent Bugnion online (Creator of MVVM Light). There's also an deepDive session available (alternatively here(at 24:47)).

In at least one of them he live codes a busy Indicator using a is BusyProperty.

Wpf cursors, Note, I am using the MVVM pattern, and I included only a portion of the XAML from In one of the WPF application, I am setting cursor to Wait cursor with below code Mouse. This event is invoked when you click the Custom Draw Crosshair Cursor MVVM and Drag and Drop Command Binding with an Attached Behavior  MVVM Wait Cursor how to set the.wait cursor during invocation of a command? (4) A very simple method is to simply bind to the 'Cursor' property of the window (or any other control).

The ViewModel should only decide whether it is busy, and the decision about what cursor to use, or whether to use some other technique such as a progress bar should be left up to the View.

And on the other hand, handling it with code-behind in the View is not so desirable either, because the ideal is that Views should not have code-behind.

Therefore I chose to make a class that can be used in the View XAML to specify that the cursor should be change to Wait when the ViewModel is busy. Using UWP + Prism the class definition is:

public class CursorBusy : FrameworkElement
{
    private static CoreCursor _arrow = new CoreCursor(CoreCursorType.Arrow, 0);
    private static CoreCursor _wait = new CoreCursor(CoreCursorType.Wait, 0);

    public static readonly DependencyProperty IsWaitCursorProperty =
        DependencyProperty.Register(
            "IsWaitCursor",
            typeof(bool),
            typeof(CursorBusy),
            new PropertyMetadata(false, OnIsWaitCursorChanged)
    );

    public bool IsWaitCursor
    {
        get { return (bool)GetValue(IsWaitCursorProperty); } 
        set { SetValue(IsWaitCursorProperty, value); }
    }

    private static void OnIsWaitCursorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        CursorBusy cb = (CursorBusy)d;
        Window.Current.CoreWindow.PointerCursor = (bool)e.NewValue ? _wait : _arrow;
    }
}

And the way to use it is:

<mvvm:SessionStateAwarePage
    x:Class="Orsa.Views.ImportPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mvvm="using:Prism.Windows.Mvvm"
    xmlns:local="using:Orsa"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mvvm:ViewModelLocator.AutoWireViewModel="True"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid>
    <Grid.RowDefinitions>
    .
    .
    </Grid.RowDefinitions>
    <local:CursorBusy IsWaitCursor="{Binding IsBusy}"/>
    (other UI Elements)
    .
    .
    </Grid>
</mvvm:SessionStateAwarePage>

🤦 Can Jsoup simulate a button click?, The major benefits of the DevExpress MVVM library are the independent In one of the WPF application, I am setting cursor to Wait cursor with below code Mouse. and can handle the command. wait cursor during invocation of a command? It’s common UI courtesy to show the Wait cursor when performing a long operation that requires the user to wait. Here is how the Wait cursor appears in Windows Vista: But developers often go about this the wrong way by setting the Cursor.Current property as follows: Cursor.Current = Cursors.WaitCursor; The problem with this approach is it

mouse position WPF MVVM, This code responds to a mouse click and selection of items from the keyboard. So, before you can configure query objects in the database, Hibernate must  I just added a wait cursor to one of our GUI's by stumbling through the docs, but I'm Stack Exchange Network Stack Exchange network consists of 177 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

How to get mouse cursor position in WPF MVVM application. Step 1: Create a new WPF project. Step 2: Create an example WPF window. MainWindow.xaml Invoke( this , new PropertyChangedEventArgs(propertyName));. }. A Neat Way to Set the Cursor in WPF Normally when you want to set the wait cursor in your application you would use a try/finally block to ensure that the cursor eventually gets set back to

The trick is to change the cursor (using addClass), then invoke the slow code with a timeout at the end of which you change the cursor back to normal.

Comments
  • How would you use it with a progressbar? (ex: modernui progress bar)
  • @sexta13 - Not sure how that works. Can try to help you if u can post a link of a sample app and describe how you want it to work.
  • When I get home later today, I'll post that question, but what've been reading I have to bind IsBusy to Visibility...but later I'll try and ask again :) Thanks in advance
  • <ProgressBar Minimum="0" Maximum="1" Height="16" IsIndeterminate="True" Margin="0,0,0,16" /> <--supposing you have this, how would you bind isbusy to visibility (for example)?
  • I am not sure why people are downvoting this answer. I got 6 downvotes today without any comments, and that too after a year and a half after posting? At least leave a comment for downvoting this so that I can improve it.
  • I think you need to change NotifyingProperty to DependencyProperty
  • Note, I am using the Apex library (apex.codeplex.com). I am defining the Cursor as a property in my ViewModel. I make it a NotifyingProperty so it can alert the View when it is changed. No need for a DependencyProperty here.
  • Agree IsBusy property on ViewModel.
  • How does this get displayed on the main window, as opposed to just on the view corresponding to the viewmodel i.e. the view is a child of the maon shell.
  • I'm not so sure. A wait cursor is just one implementation of a busy indicator. The ViewModel should not know about View implementation. What if the view wanted to denote busyness in some other way such as an animation or simple text message? A simple IsBusy property allows the view to implement how it wants.
  • @GazTheDestroyer: That is true, never thought of it like that.