locked
MVVM command binding RRS feed

  • Question

  • hello,

    i am writing using MVVM.

    I have a button and i am using a command binding.

    I want that wait cursor will be displayed while command is executed. when command is finished - i want the cursor to be default. How can i do it? 

    Ofcourse i can't change the cursor in my VM.

    Friday, July 13, 2012 10:29 AM

Answers

  • In order for have a simple strategy, let say you are doing busy work in VM using background worker thread. When you start the work (DoWork) set IsBusy (This is a property which I am proposing to expose in your VM and bind it to DependencyObject cursor. See this) and define a custom value converter which convert from a boolean state to cursor enum/value. Eg:

    <Grid Cursor={Binding IsBusy, Converter={StaticResource BooleanToCursorConverter}}


    If this post answers your question, please click Mark As Answer. If this post is helpful please click Mark as Helpful.

    Regards,
    Nair S

    • Proposed as answer by Pete LakerMVP Friday, July 13, 2012 11:13 AM
    • Marked as answer by Annabella Luo Tuesday, July 24, 2012 7:20 AM
    Friday, July 13, 2012 10:43 AM
  • Step 1) Learn how to change the Cursor type in the Mainwindow's code behind by testing a click event of a button.

    Step 2) In your ICOMMAND object wire up an event named something like OnCursorChange it would look similar to this:

    public delegate void OnCursor(Cursors.Wait cursorType); public static event OnCursor OnCursorChange;

    //Then fire the event like this in the Execute method of the command...

    SomeCursortype= Cursors.Wait;

    //How to fire the event

    if(OnCursorChange!=null){ OnCursorChange(SomeCursorType);

    //Then execute your logic Aysnchronously and have that thread fire another event to turn it off.

    Step 3) Go to the MainWindow's code behind and in the initialization logic wire up an event handler like this:

    //How to wire up an EventHandler... MyCommandClass.OnCursorChange += new delegate(MyCursorHandler); private void MyCursorHandler(Cursor cursorType){ SomeControl.DisplayArea.Cursor = cursorType; }

    Note you may have to study a bit to figure out how anybody can fire the cursor event just by calling a static method somewhere. That way you have created an abstract place one time in one place which is good for your code.

    JP Cowboy Coders Unite!

    • Marked as answer by Annabella Luo Tuesday, July 24, 2012 7:40 AM
    Friday, July 13, 2012 10:47 AM

All replies

  • In order for have a simple strategy, let say you are doing busy work in VM using background worker thread. When you start the work (DoWork) set IsBusy (This is a property which I am proposing to expose in your VM and bind it to DependencyObject cursor. See this) and define a custom value converter which convert from a boolean state to cursor enum/value. Eg:

    <Grid Cursor={Binding IsBusy, Converter={StaticResource BooleanToCursorConverter}}


    If this post answers your question, please click Mark As Answer. If this post is helpful please click Mark as Helpful.

    Regards,
    Nair S

    • Proposed as answer by Pete LakerMVP Friday, July 13, 2012 11:13 AM
    • Marked as answer by Annabella Luo Tuesday, July 24, 2012 7:20 AM
    Friday, July 13, 2012 10:43 AM
  • Step 1) Learn how to change the Cursor type in the Mainwindow's code behind by testing a click event of a button.

    Step 2) In your ICOMMAND object wire up an event named something like OnCursorChange it would look similar to this:

    public delegate void OnCursor(Cursors.Wait cursorType); public static event OnCursor OnCursorChange;

    //Then fire the event like this in the Execute method of the command...

    SomeCursortype= Cursors.Wait;

    //How to fire the event

    if(OnCursorChange!=null){ OnCursorChange(SomeCursorType);

    //Then execute your logic Aysnchronously and have that thread fire another event to turn it off.

    Step 3) Go to the MainWindow's code behind and in the initialization logic wire up an event handler like this:

    //How to wire up an EventHandler... MyCommandClass.OnCursorChange += new delegate(MyCursorHandler); private void MyCursorHandler(Cursor cursorType){ SomeControl.DisplayArea.Cursor = cursorType; }

    Note you may have to study a bit to figure out how anybody can fire the cursor event just by calling a static method somewhere. That way you have created an abstract place one time in one place which is good for your code.

    JP Cowboy Coders Unite!

    • Marked as answer by Annabella Luo Tuesday, July 24, 2012 7:40 AM
    Friday, July 13, 2012 10:47 AM