dotnetmonitor.com

 
Index
Previous
Next

 

Property Change Notification

The single most important concept to understand about Windows Forms data binding is that it is driven off of change notification. What this means is that Windows Forms will not update a user interface element (Control) unless that data source notifies the Windows Forms data binding runtime that the data has changed (by providing a notification event). In the case of simple property to property binding, the data source needs to provide property change notification by either providing a PropertyNameChanged event for the property or by implementing the INotifyPropertyChanged interface. The INotifyPropertyChanged interface is new in VS 2005 and can be used with BindingList<T> to create bindable lists (described below).

Sample: Simple Binding without Change Notification (VS 2005) (VS Project: PropertyChangeNotification)

/*******************************************************************

* Setup (using the Visual Studio Form designer):

*

* Add a Label to the Form (label1)

* Add the following code to the Form.Load event

******************************************************************/

 

CurrentTime currentTime = new CurrentTime();

 

/*******************************************************************

* Bind Label.Text (string) to CurrentTime.Now

* Note: the Label will not update when Now changes because Now

* does not provide change notification.

******************************************************************/

 

Binding binding = new Binding("Text", currentTime, "Now", true);

binding.FormatString = "MM/dd/yyyy hh:MM:ss";

 

this.label1.DataBindings.Add(binding);

 

/***********************************************************************

* Setup (using the Visual Studio Form designer):

*

* Add a new class file to your project named "CurrentTime"

* Add the following code to the class implementation

**********************************************************************/

 

public class CurrentTime

{

System.Windows.Forms.Timer _timer;

 

public CurrentTime()

{

/***************************************************************

* Use a timer to keep track of the current timer

***************************************************************/

_timer = new System.Windows.Forms.Timer();

 

/***************************************************************

* Update the time every second

***************************************************************/

 

_timer.Interval = 1000;

_timer.Tick += delegate { this.Now = DateTime.Now; };

_timer.Start();

}

 

/*******************************************************************

* Use a timer to keep track of the current time

*******************************************************************/

 

private DateTime _now = DateTime.Now;

 

/*******************************************************************

* No Change Notification bound controls will not update

*******************************************************************/

 

public DateTime Now

{

get { return _now; }

private set

{

if (_now != value)

{

_now = value;

}

}

}

}

Simple Binding
Complex Binding
Using Simple Binding with a List
Sample: Simple Binding to a List (VS 2005) (VS Project: DataBinding Intro)
Type Conversion
Formatting