Data binding

The Menu screen is static and does not change according to some additional data coming from the UI elements or even the web service. However, other pages need to manage various data and present them in the user interface. One of the very useful concepts for the creation of such screens is the data binding mechanism. By using this, you can decide how particular data are presented on the page, as well as how to handle their changes.

This means that you can automatically reflect changes made in the source (for example, text in a TextBox control) into the target (for example, content shown in a TextBlock control), as well as even update the source depending on modifications made in the target.

According to http://msdn.microsoft.com/library/cc278072, the target is a dependency property of the FrameworkElement instance (that is, any control placed on the page), while the source is a property of some object (not necessarily from the UI). The basic concept of the data binding mechanism is shown as follows:

Data binding

The binding can be performed in one out of three modes: one time, one way, and two way. In the first approach, the source is presented in the target only once—when the binding is created. The one way is the default binding mode and it updates the target whenever the source is changed. The most complex mode is the two way, because it updates the target whenever the source is changed, as well as reflects changes from the target to the source.

You can specify the binding in XAML using the Binding object, which is placed as a value of the target—a dependency property of the UI element. It can specify a few settings including a name of the element used for binding (ElementName), a path to the source property (Path), a binding mode (Mode), a format (StringFormat), and a value presented when the binding evaluates to null (TargetNullValue).

As an example, you can use the data binding mechanism to present a value from the TextBox in TextBlock control. Thus, the source is Text property of the TextBox control, while the target is the Text property of the TextBlock:

<TextBox x:Name="txtSource" Text="Default text" Height="100" />
<TextBlock Text="{Binding ElementName=txtSource, Path=Text}" />

The Text property of the TextBlock control is bound to the Text property (Path) of the txtSource element (ElementName). The Binding object is specified using the attribute syntax for the target property (Text of TextBlock control). The binding can be also created in a slightly different form, where the path is defined without the Path property name. It provides you with a shorter way of specifying the binding, as shown in the following code snippet:

<TextBlock Text="{Binding Text, ElementName=txtSource}" />

The mechanism of data binding can be used in many scenarios, for example, to present data stored as values of the class properties. Thus, you will use it frequently in this book and many additional examples will be shown and explained later.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.218.69