WPF-DataBinding是否支持异步更新
我意外更改了从 BackgroundWorker
(BackgroundWorker.DoWork
) 中实现 INotifyPropertyChanged
的 BusinessObject 属性的值。
令人惊讶的是,这并没有导致错误,而是实现了绑定到属性的 TextBlock 文本,没有任何抱怨。
异步绑定的执行是 WPF 绑定引擎的一部分,还是这只是由于其他考虑而忘记或省略 CheckAccess 测试的特殊情况。
I changed accidentally the value of a BusinessObject-property that implements INotifyPropertyChanged
from within a BackgroundWorker
(BackgroundWorker.DoWork
).
Amazingly, this led not to an error but actualized the text of the TextBlock that was bound to the property without any complaint.
Is the execution of asynchronous bindings part of the WPF binding engine or is this only a special case where the CheckAccess-test have been forgotten or ommited due to other considerations.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
标量属性上的绑定支持来自其他线程的更新,因此在更新模型(或 ViewModel)的属性时无需调用 Dispatcher.Invoke。但是,对于绑定到集合而言,情况并非如此:如果您将
ItemsControl
绑定到ObservableCollection
,则对此集合的更改必须 在 UI 线程上完成,因为CollectionChanged
事件不会自动转发到 UI 线程。或者,您可以使用ObservableCollection
的变体来引发 UI 线程上的事件 (这是一个实现)Bindings on scalar properties support updates from other threads, so you don't need to call
Dispatcher.Invoke
when updating a property of the model (or ViewModel). However, this is not true for binding to a collection: if you have aItemsControl
bound to anObservableCollection<T>
, changes to this collection must be done on the UI thread, asCollectionChanged
events are not automatically forwarded to the UI thread. Alternatively, you could use a variant ofObservableCollection<T>
that raises the event on the UI thread (here's a implementation)