views:

27

answers:

1

Is there any way to pause all of the bindings on a window, or pause all bindings in the entire program at runtime?

I have a class called Page, it contains a lot of variables that my controls bind to in order to update and be updated by the class. I have a load function that loads an XML file and creates a Page class from it. The problem is that as it does that, all of the databindings attempt to update at the same time, causing massive slowdown on the dispatcher. This makes it hell to run asynchronously as while the code might be running asynchronously, the UI is still freezing due to all the binding updates going on.

Is there anyway to pause or freeze all the bindings and then unfreeze afterwards?

+3  A: 

You could implement a latching mechanism on Page that suppresses change notifications:

public class Page : INotifyPropertyChanged
{
    private bool areNotificationsSuppressed;

    public IDisposable SuppressNotifications()
    {
        return new NotificationSuppression();
    }

    protected virtual void OnPropertyChanged(...)
    {
        if (this.areNotificationsSuppressed)
        {
            return;
        }

        ...
    }

    private sealed class NotificationSuppression : IDisposable
    {
        // set areNotificationsSuppressed to true, and then false once disposed
        // queue up any notifications and fire them after disposal
    }
}

Alternatively, you could just remove the object from the DataContext whilst you make changes to it, and then assign it back to your DataContext once done.

HTH,
Kent

Kent Boogaart
Thanks, that's perfect.
Nick Udell