WPF如何等待处理更多的代码之前发生绑定更新? [英] WPF how to wait for binding update to occur before processing more code?

查看:88
本文介绍了WPF如何等待处理更多的代码之前发生绑定更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解调度发生在另一个线程,它负责更新数据绑定,布局等。但是,有没有办法等到调度员没有更多​​的项目,或者至少没有更多的数据绑定?我想,以确保财产变化更新所有的组件,然后运行依赖房地产运行更多的代码,一改之前的回调

As I understand it the dispatcher takes place in another thread, which takes care of updating data bindings, layout, etc. However, is there any way to wait until the dispatcher has no more items or at least no more data bindings? I want to ensure that the property change has updated all of its components and run the dependent property changed callbacks before running more code.

编辑:所以我猜测,这是没有必要,我只是想明白我应该做的,而不是。我的主要问题是 WPF如果一个ScrollViewer中的儿童重新调整,将ScrollViewer中自动更新它的程度?

edit: so I am guessing that this is not needed, I am just trying to understand what I should have done instead. My main question is at WPF if the children of a scrollviewer resize, will the scrollviewer automatically update its extent?

但我也很好奇我是否可以等待绑定更新或是否有甚至任何保证之前,另一个绑定更新?难道我预计代码,这样绑定更新的顺序并不重要?目前我使用的依赖属性更改回调的执行各种东西,往往是依赖于其他属性进行更新

but I was also curious whether i could wait for bindings to update or if there is even any guarantee that one binding updates before another? Am I expected to code such that the order of binding updates do not matter? currently I used dependency property changed callback's that perform various stuff that is often dependent on other properties updating

推荐答案

调度程序有几项工作重点处理单任务。我敢肯定你不能改变依赖属性回调的优先级。如果他们通过数据绑定改变,他们将在队列DispatcherPriority.DataBinding优先级。
你处理一些依赖属性这将导致布局/其他依赖属性改变的值。你想,直到这些布局更新/改变处理您的第一个操作后等待,然后做与当前的UI状态的东西吗?

The dispatcher has several priorities for processing the single tasks. I'm quite sure you cannot change the priority of the callbacks of dependency properties. If they were changed by data binding, they will be in queue with DispatcherPriority.DataBinding priority. You manipulate the values of some dependency properties which would cause layout/other dependency properties to change. And you want to wait after your first manipulation until these layout updates/changes are processed and then do something with the current UI state?

的WinForms有这样的情况下,这会导致正常的代码执行继续之前处理的所有UI事件的DoEvents功能。 WPF没有这样的内置函数,但你可以编写自己的DoEvents:

WinForms had the DoEvents function for such cases which causes all UI events being processed before normal code execution continues. WPF has no such builtin function but you can write your own DoEvents:

public static void DoEvents()
{
  if (Application.Current == null)
    return;
  Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, (Delegate) (() => {}));
}



空委托同步调用,这意味着调用返回后,该委托是由调度员执行。该DispatcherPriority.Background是一个非常低优先级,以便该调度呼叫处理后,例如数据绑定,布局更新或渲染。
http://msdn.microsoft.com /en-us/library/system.windows.threading.dispatcherpriority.aspx

您的依赖属性和之前第一次处理后,于是呼的DoEvents()你想读其他的依赖属性,它应该做的事。

So call DoEvents() after your first manipulation of the dependency properties and before you want to read other dependency properties and it should be done.

这篇关于WPF如何等待处理更多的代码之前发生绑定更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆