Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to listen Binding.SourceUpdated on all children of a root element?

I want to listen Binding.SourceUpdated on all the child bindings defined.

http://msdn.microsoft.com/en-us/library/system.windows.data.binding.sourceupdated.aspx says

Set the NotifyOnTargetUpdated or NotifyOnSourceUpdated property (or both) to true in the binding. The handler you provide to listen for this event must be attached directly to the element where you want to be informed of changes, or to the overall data context if you want to be aware that anything in the context has changed.

That means we should be able to listen those event per DataContext instead of per Binding element.

like image 231
Rohit Avatar asked Aug 20 '11 15:08

Rohit


1 Answers

Like most WPF events, SourceUpdated is a Routed Event. Any event handler for this event placed on a given element will also be called when a child element raises this event.

If you have the following code:

<StackPanel Binding.SourceUpdated="OnBindingSourceUpdated">
  <TextBlock Text="{Binding Path=A, NotifyOnSourceUpdated=True}" />
  <TextBlock Text="{Binding Path=B, NotifyOnSourceUpdated=True}" />
</StackPanel>

The handler OnBindingSourceUpdated will handle binding source changes for both textboxes. Place the attached event handler on the element where the data context is originally defined and you'll get notifications for every source change.

like image 134
Julien Lebosquain Avatar answered Oct 10 '22 09:10

Julien Lebosquain