At times, we may need to make changes to our binding sources and have those changes propagate to the binding target controls. We may want to set default values on a new form, clear old form values, or even set form labels from our View Models. In order to do this, our View Models must implement the INotifyPropertyChanged interface and this is why we build this implementation into our base View Model class.
When we data bind a binding source to a control in the UI, an event handler is attached to the PropertyChanged event of the source object. When a notification of a change to the property that is specified by the binding source property path is received, the control is updated with the new value.
It should be noted that the PropertyChanged event of the binding source will be null if no handler has specifically been attached and none of its properties have been data bound to UI controls. It is for this reason that we must always check for null, before raising this event.
All of the binding modes work in the direction of binding source to binding target, except for the OneWayToSource instance. However, only this and the TwoWay instance of the Binding.Mode enumeration propagate changes in the direction of the binding target to the binding source.
When the binding is working in either of these modes, it attaches a handler to the target control to listen for changes to the target property. When it receives notification of a change to the target property, its behavior is determined by the value of the binding's UpdateSourceTrigger property.
This property is of the enumeration type UpdateSourceTrigger, which has four members. The most common is the PropertyChanged instance and this specifies that the source property should be updated as soon as the target property has changed. This is the default value for most controls.
The LostFocus member is the next most common value and this specifies that the binding should update the binding source when the user moves focus from the data bound control. This option can be useful when we want to trigger validation once the user has completed entry in each textbox, rather than as they type.
The Explicit instance will not update the binding source without explicit instruction to do so. As we need to programmatically call the UpdateSource method of the internal BindingExpression object in order to propagate the changes to the binding source, this option is not generally used in our normal Views.
Instead, if used at all, we would find it in our CustomControl classes. Note that calling the UpdateSource method will do nothing if the binding mode is not set to one of the OneWayToSource or TwoWay instances.
If we had an instance of a textbox and we wanted to explicitly update the binding source that was data bound to its Text property, we can access the lower-level BindingExpression object from the BindingOperations.GetBindingExpression method and call its UpdateSource method:
BindingExpression bindingExpression = BindingOperations.GetBindingExpression(textBox, TextBox.TextProperty); bindingExpression.UpdateSource();
Alternatively, if our binding target control class extends the FrameworkElement class and most do, then we can simply call the GetBindingExpression method on it directly and pass in the Dependency Property key that we want to update the binding from:
textBox.GetBindingExpression(TextBox.TextProperty);
The last member of the UpdateSourceTrigger enumeration is the Default instance. This is similar to the Default instance of the Binding.Mode enumeration in that it uses the value specified by each target Dependency Property and is the default value of the UpdateSourceTrigger property. Again, we'll find out how to set the metadata for Dependency Properties later in this chapter.