The Binding Class
In Windows Presentation Foundation, data binding is accomplished by a binding object that sits between the binding target (the control) and the binding source (the data object):
It is this Binding object that we are defining the properties of in the XAML attribute with "{Binding}"
. Hence, Path
is a property defined on this binding.
As we mentioned before, bindings can be OneWay
or TwoWay
based on the direction the data flows. The binding mode is specified by the Binding
object’s Mode
property, which can also be set in XAML. There are actually two additional options. The first is a OneWayToSource
that is basically a reversed one-way binding (the control updates the data object, but the data object does not update the control)
For example, we actually could use a <TextEditor>
with a read-only property, if we changed the binding mode:
<TextEditor Text="{Binding Path=FullName Mode=OneWay}" />
Though this might cause your user confusion because they would seem to be able to change the property, but the change would not actually be applied to the bound object. However, if you also set the IsEnabled
property to false to prevent the user from making changes:
<TextEditor Text="{Binding Path=FullName Mode=OneWay}" IsEnabled="False" />
The second binding mode is OneTime
which initializes the control with the property, but does not apply any subsequent changes. This is similar to the behavior you will see from a data object that does not implement the INotifyPropertyChanged
interface, as the Binding
object depends on it for notifications that the property has changed.
Generally, you’ll want to use a control meant to be used with the mode you intend to employ - editable controls default to TwoWay
and display controls to OneWay
.
One other property of the Binding
class that’s good to know is the Source
property. Normally this is determined by the DataContext
of the control, but you can override it in the XAML.