How Data Binding Works? - An Overview

The data binding is the connection between the application UI and business logic. There are lots of changes in data binding mechanism from .NET 2.0 to .NET 3.0. Currently the data binding mechanism can be described using the XAML language. There are new classes like 'Binding' which are introduced for data binding. The data binding source can be anything ranging from common language runtime (CLR) objects to XML files.



How the Data binding works?

Binding Target

A binding target could be a ContentControls such as Button or ItemsControls such as ListBox/ListView.

Any properties of binding target like background color or content can be bound to binding source. This is the flexibility of the WPF. For example, in inventory application background color of the list item can be set to red or green depending on stock qty.

Binding source

The binding source is data provider like data base object or XML files.

Binding Object

The Binding is the class, which bridges the Binding source and Binding target.
Binding objects supports different directions of data flow. They are:
One Way - data flow from data source to data target.
Two Way - data flow in both direction, when ever target value changed, the source get updated also
One Way to Source - The data flow allowed only in the reverse direction
One Time - the data flow from the binding source to binding target only once(at the time of initiation target).

The property 'UpdateSourceTriger' must be set, which determine when data should flow from binding source to binding target. The below table show the possible values for this property.

Valid Value forUpdateSourceTrigger'
Description
Example
LostFocus (deault)
Source get updated when control lost its focus
Used when the control input must be validate on lost focus
PropertyChanged
Source get update when the controls content property get changed
Online messenger
Explicit
Source get updated when the application request
updates the source only when the user clicks the submit button

Sample:

The example below shows how to bind background color of the button.

<DockPanel>
<DockPanel.Resources>
<c:MyData x:Key="propSource1"/>
</DockPanel.Resources>
<DockPanel.DataContext>
<Binding Source="{StaticResource propSource1}"/>
</DockPanel.DataContext>
<Button Width="150" Height="30" Background="{Binding Path=ColorName}" >Red<Button>
</DockPanel>

Specifying the Path to the Value

For object data source Path property can be used for specify the binding. For XML data, XPath propertycan be used. In some cases, it may be applicable to use the Path property even when your data is XML. XPath and Path both can be used specify data source, where Path can used to specify a property in the XML Node returned by the XPath property.

Below is an example for data binding, where the List Control's 'ItemSource' property is used to set the binding property.

XAML file:
<DockPanel>
<DockPanel.Resources>
<XmlDataProvider x:Key="Books" Source="XMLFile1.xml" />
</DockPanel.Resources>
<ListBox Name="bookListBox"
Height="600"
ItemsSource="{Binding Source={StaticResource Books}, XPath=//title}"
/>
</DockPanel>

XML File:
<books>
<book isbn="034537">
<title>Story1</title>
<author>Eva Lida</author>
</book>
<book isbn="034538">
<title>Songs1</title>
<author>Corets</author>
</book>
<book isbn="034539">
<title>Poetry</title>
<author>Minu</author>
</book>
</books>

The above example illustrates how to bind the collection of data items to the list box.


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.