Is any event triggered before the source of a WPF binding is updated?
With the following WPF TextBox
, TextBox_SourceUpdated
is invoked after the source of the binding has been updated:
<TextBox Text="{Binding Path=title, NotifyOnSourceUpdated=True}"
SourceUpdated="TextBox_SourceUpdated" />
Is there an event that is triggered before the source of the binding gets updated?
UPDATE
I already played around with invoking GetBindingExpression(TextBox.TextProperty).UpdateSource()
from the LostFocus
event combined with UpdateSourceTrigger=Explicit
. I am very dissatisfied with this approach as I end up having to:
- verify that the
DataContext
is not disconnected - verify that
IsLoaded == true
- verify if the value has changed at all to begin with
This is painful, so I'm looking for something else; something simpler.
2nd UPDATE
So I gave up. I decided to revert to using the SourceUpdated event (triggered after the source is updated as the name implies) and always keeping a sqlite savepoint before any of these source update 开发者_运维百科can happen, allowing to always go back exactly before the update happened.
I don't think there's anything you can do directly with the Binding events. Depending on what you're trying to achieve, some combination of the TextChanged and PreviewLostKeyboardFocus events might do what you need. Both of those events will happen before SourceUpdated. The TextChanged event will fire on every key entry but with the default UpdateSourceTrigger of LostFocus PLKF will fire once right before the binding pushes updates and SourceUpdated fires.
The only way I could think of to simulate such a feature would be to do a double binding with a proxy property. For example:
Property A: {Binding ABProxy, Mode=TwoWay}
Property B: {Binding ABProxy, Mode=TwoWay}
Property ABProxy: (used as proxy, binding source and target)
You could then listen on A updating its source to do something before B is updated, or listen for the Proxy changing, etc.
If you had a one way binding, you could just bind to the proxy property, and in your after update, decide whether to propagate the change to property B.
精彩评论