none
How can I force a binding update? RRS feed

  • Question

  • I'ld like to force a binding to update, when raising the PropertyChangedEvent. The binding doesn't update, because the main thread is busy. Is it possible to do that or do I have to move all the heavy-cpu stuff to another thread?
    Thanks in advance.
    Tuesday, March 24, 2009 7:26 PM

Answers

  • I'm sorry to say so, but none of you're solutions worked. I already had the INotifyPropertyChanged implemented. I tried to update de cursor. I tried to create a separate tread and join the main thread, but neither one updated the interface. The only difference using a seperate thread and join is that the UI won't go black.
    My final solution is to run a different thread, without letting the main thread wait (join). Bind "IsEnabled" to a dependency property and update that when the 2nd tread has finished. I'm really surprised with this behavior, because binding is suposed to have a higher prioriy.
    • Marked as answer by _patrick_ Tuesday, March 31, 2009 8:18 PM
    Tuesday, March 31, 2009 8:17 PM

All replies

  • All cpu intensive work should be done off of the UI thread if at all possible.  This will speed up the responsiveness of your UI
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Tuesday, March 24, 2009 7:32 PM
  • I know, but in this case it isn't worth the hassle. I'm just talking about a few seconds of unresposiveness. I just wanted to show a message so the user knows what the program is doing.
    Wednesday, March 25, 2009 6:58 PM
  • You can force the update of the binding in the following way:

    BindingExpression expression = control.GetBindingExpression(<DependencyProperty>);
    if (expression != null)
    {
        expression.UpdateTarget();
    }

    Wednesday, March 25, 2009 7:13 PM
  • Thanks Mariano, although I knew that one. What I'm looking for is a way so the class can force the controls to update. The class should find out what controls are binded to that particular value, and force those controls to update. So it's more or less what you put up, but the other way arround.
    Thursday, March 26, 2009 11:55 AM
  • Hi,


    -->The class should find out what controls are binded to that particular value, and force those controls to update.


    You can make your class implemented
    INotifyPropertyChanged interface. As a consequence, the UI will reflect the changes in data source automatically without updating your data binding explicitly.

     

    Hope this helps.

    Thanks.


    Jim Zhou -MSFT
    Monday, March 30, 2009 3:44 AM
  • Patrick, I sense your pain, this is annoying for me too. It
    would be great if you could just do a:

    textbox.Text = "Please wait...";
    
    // refresh the form somehow
    
    // do lots of complicated code
    
    textbox.Text = "Done...";
    
     
    Plus I also doubly sense your pain because everyone is ignoring your actual request :)

    However what you can do is set the mouse cursor to an hourglass:

    System.Windows.Input.Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
    And then set it back in a finally block:

    System.Windows.Input.Mouse.OverrideCursor = null;

    The app looks like it's a lot more responsive this way too.

    But far as I'm aware you can't do what you're trying to do.
    Your code is running, therefore no UI updating until the code
    has finished. It has to go into another thread.
    • Proposed as answer by clockwise_music Monday, March 30, 2009 5:43 AM
    • Marked as answer by Jim Zhou - MSFT Monday, March 30, 2009 6:02 AM
    • Unmarked as answer by _patrick_ Tuesday, March 31, 2009 8:10 PM
    • Unproposed as answer by _patrick_ Tuesday, March 31, 2009 8:10 PM
    Monday, March 30, 2009 5:43 AM
  • I'm sorry to say so, but none of you're solutions worked. I already had the INotifyPropertyChanged implemented. I tried to update de cursor. I tried to create a separate tread and join the main thread, but neither one updated the interface. The only difference using a seperate thread and join is that the UI won't go black.
    My final solution is to run a different thread, without letting the main thread wait (join). Bind "IsEnabled" to a dependency property and update that when the 2nd tread has finished. I'm really surprised with this behavior, because binding is suposed to have a higher prioriy.
    • Marked as answer by _patrick_ Tuesday, March 31, 2009 8:18 PM
    Tuesday, March 31, 2009 8:17 PM