Directing data bound traffic

The direction of data traversal in each binding is specified by the Binding.Mode property. There are four distinct directional instances declared in the BindingMode enumeration, plus an additional value. Let's first take a look at the directional values and what they represent.

The first and most common value reflects the most common situation, where data flows from the binding source, say, one of our View Models, to the binding target, represented by a UI control. This binding mode is called One-Way and is specified by the OneWay enumeration instance. This mode is used primarily for display only, or read-only purposes, and situations where the data bound values cannot be altered in the UI.

The next most common direction of travel is represented by the TwoWay enumeration instance and signifies that data is free to travel both from our View Models to the UI controls and also in the opposite direction. This is the most commonly used mode when data binding to form controls, when we want the users' changes to be reflected in our View Models.

The third directional enumeration instance is the OneWayToSource instance and is the opposite to the OneWay instance. That is, it specifies that data can only travel from the binding target, represented by a UI control, to the binding source, for example, one of our View Models. This mode is also useful for capturing user inputted date, when we don't need to alter the data bound values.

The final directional instance is similar to the OneWay instance, except that it only works once and is represented by the OneTime instance. While this mode will indeed only work one time, upon instantiation of its containing control, it will actually also update its value each time the DataContext property of the relevant binding is set. However, its purpose is that it provides better performance than the OneWay member and is only suitable for binding to non-changing data, so if the data will be updated, this is not the correct directional instance to use.

The final instance is named Default and as the name hints, is the default value of the Binding.Mode enumeration. It directs the binding to use the binding mode that was declared from the specified target property. When each Dependency Property is declared, we can specify whether a One or Two-Way binding mode should be used by default. If this is not specifically declared, then the property will be assigned a One-Way mode. We'll see this explained in more detail later in this chapter.