BindingExpression的来源发生变化时的通知? [英] Notification when the Source of a BindingExpression changes?

查看:117
本文介绍了BindingExpression的来源发生变化时的通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TestScenario:

TestScenario:

我有一个具有DependencyProperty的控件A.
我有一个ViewModel,其属性A与我绑定Control的A属性使用OneWayToSource绑定。绑定由控件显式更新。

I've got a Control with a DependencyProperty A. I've got a ViewModel with a property A that I bind against the Control's A-Property using the OneWayToSource Binding. The Binding is updated explicitly by the Control.

我在运行时切换了绑定的ViewModel实例,从而更改了我的绑定的基础源。当这种情况发生时,我想要一个事件触发,告诉我的控制,我现在可以更新其DependencyProperty A的值。原因是,一旦更改了绑定的ViewModel,DependencyProperty A的DefaultValue被写入到源头。
相反,我希望控件提出一个适当的值,并手动更新源。

I switch out the bound ViewModel instance at runtime, thereby changing the underlying Source of my Binding. When this happens I would like an Event to fire that tells my control that I can now update the value of its DependencyProperty A. The reason is, that as soon as you change the bound ViewModel, the DependencyProperty A's DefaultValue is written to the Source. Instead, I would like the control to come up with a proper value and update the source manually.

有人可能会认为只要听DataContextChanged事件才能解决问题。但是,当这个事件被触发时,BindingExpression的Source仍然指向旧的ViewModel。

One might think that just listening to the DataContextChanged event solves the problem. However, when this Event gets fired, the Source of the BindingExpression still points to the old ViewModel.

我可以想出的唯一方法是使用Dispatcher.BeginInvoke在DataContextChanged的EventHandler中使用DispatcherPriority.DataBind优先级。

The only hackaround I can come up with, is to use Dispatcher.BeginInvoke with the DispatcherPriority.DataBind priority in the EventHandler of DataContextChanged.

它的工作原理,但对我来说不太干净。

It works, but does not feel clean to me.

我正在寻找一个绑定类中的事件,通知我关于源的更改,但是找不到。

I am looking for an event in the Binding-class that notifies me about changes of the Source... however I cannot find one.

谢谢


Thank you

推荐答案

也许我完全不了解这个问题,但你可以使用 NotifyOnSourceUpdated NotifyOnTargetUpdated 属性(和相应的 SourceUpdated TargetUpdated events)?

Perhaps I'm not understanding the question completely, but can you just use the NotifyOnSourceUpdated or NotifyOnTargetUpdated properties (and the corresponding SourceUpdated or TargetUpdated events)?

这篇关于BindingExpression的来源发生变化时的通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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