Programming - Silverlight - Data binding
Data Binding
Data binding is a connection between the User Interface and a
business object or other data provider. The User Interface
object is called the target, the
provider of the data is called the source.
Data-binding assists with the separation of the User
Interface level of your application from the other layers
ofand your application (business objects, data, and so
forth). This separation of responsibility is further
reinforced by decoupling the UI target from its source
through the the use of a Binding object.
The binding object can be thought of as a black box with a
universal connectors on one side for the target and on the
other side for the source. There are switches on top, the
most important of which is the Data Binding Mode switch which
determines which way the data will flow.
OneTime binding sets the target and
then the binding is completed. This is great for displaying
data that rarely or never changes
OneWay bindingand sets the
target and keeps it up to date as the source changes. This is
great for displaying data that the user is not permitted to
change.
TwoWay binding sets the target and
keeps the target up to date as the source changes and keeps
the source up to date as the user changes the target or
changes something else in the application that will cause
your application to change the source.
If you were building an on line bookstore and were displaying
information about a book, you might use OneTime binding for
the Title and Author (once you get them, they are not going
to change) and OneWay binding for the price (someone might
mark down the price during the day) but youd want TwoWay
binding for the Quantity on hand.
The target of your binding can be any public property of
virtually any CLR object (for the minor restrictions on this,
see the Afterward).
You can see this by building a small example; but even in
this stripped down exercise, well keep to a basic 3-tier
approach being sure to keep a relatively strict separation
among
-
User Interface Layer
-
Business Layer
-
Persistence Layer
What separates a
Silverlight Business object from one created for (e.g.,
ASP.NET) is that we want to have our business object
participate in oneway or twoway binding with the UI layer. To
do so, it must implement the INotifyPropertyChanged interface. It
allows us to bind objects directly to a control, which
implements a very strict MVC pattern.
public abstract class NotifyProperyChangedBase :
INotifyPropertyChanged
and {
and #region
INotifyPropertyChanged Members
and public event PropertyChangedEventHandler
PropertyChanged;
and #endregion
and #region methods
and protected bool
CheckPropertyChanged<T>(string propertyName, ref T oldValue, ref T newValue)
and {
and
if (oldValue == null && newValue == null)
and
{
and
return false;
and
}
and
if ((oldValue == null && newValue != null) ||
!oldValue.Equals((T)newValue))
and
{
and
oldValue = newValue;
and
FirePropertyChanged(propertyName);
and
and
return true;and
and
}
and
return false;
and }
and protected void FirePropertyChanged(string propertyName)
and {
and
if (this.PropertyChanged != null)
and
{
and
this.PropertyChanged(this, new
PropertyChangedEventArgs(propertyName));
and
}
and }
and #endregion
and }
More information can be found
here.
Example code can be found here.