We manually write code to keep the UI and data synchronized. Effectively binds two sets of attributes together implicitly, one from the person object and the other from the control that displays the person object. Data binding is used to explicitly bind properties from one object to another, keep them synchronized, and convert to the appropriate type, as shown in Figure 4-7.
Figure 4-7
4.2.1 Binding
Instead of manually setting the Text property of the TextBox object in code and guaranteeing that they are up to date, data binding allows us to use an instance of the binding object to set the Text property, as shown in example 4-8.
Example 4-8
<TextBox >
<TextBox.Text>
<Binding Path="Age" />
</TextBox.Text>
</TextBox>
In example 4-8, we have used the property element syntax described in the first chapter to create an instance of the binding class, initialize its Path property to "age", and set the binding object to the Text property value of the TextBox object. Using the binding label (also introduced in the first chapter), we can sample 4-8 abbreviated to Example 4-9.
Example 4-9
<TextBox TextContent="{Binding Path=Age}" />
As a shorter abridged version, you can omit the specified path together, and binding can also see what that means, as in example 4-10.
Example 4-10
<TextBox TextContent="{Binding Age}" />
I prefer to display syntax declarations, so I don't use the syntax of example 4-10, but I don't comment--if you like.
The binding class provides a variety of interesting tools for managing bindings between attributes, but we are more concerned with the path attribute. Most occasions, you might think of path as the property name of an object, as a data source. Therefore, the binding statement of example 4-10 establishes the binding between the Text property of a TextBox and the Name property of an object, as shown in Figure 4-8.
Figure 4-8