开发者

Pause bindings for entire window?

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜