Udostępnij za pośrednictwem


Change Notification in Windows Forms Data Binding

One of the most important concepts of Windows Forms data binding is change notification. To ensure that your data source and bound controls always have the most recent data, you must add change notification for data binding. Specifically, you want to ensure that bound controls are notified of changes that were made to their data source, and the data source is notified of changes that were made to the bound properties of a control.

There are different kinds of change notification, depending on the kind of data binding:

  • Simple binding, in which a single control property is bound to a single instance of an object.

  • List-based binding, which can include a single control property bound to the property of an item in a list or a control property bound to a list of objects.

Additionally, if you are creating Windows Forms controls that you want to use for data binding, you must apply the PropertyNameChanged pattern to the controls, so that changes to the bound property of a control are propagated to the data source.

Change Notification for Simple Binding

For simple binding, business objects must provide change notification when the value of a bound property changes. You can do this by exposing an PropertyNameChanged event for each property of your business object and binding the business object to controls with the BindingSource or the preferred method in which your business object implements the INotifyPropertyChanged interface and raises a PropertyChanged event when the value of a property changes. For more information, see How to: Implement the INotifyPropertyChanged Interface. When you use objects that implement the INotifyPropertyChanged interface, you do not have to use the BindingSource to bind the object to a control, but using the BindingSource is recommended.

Change Notification for List-Based Binding

Windows Forms depends on a bound list to provide property change (a list item property value changes) and list changed (an item is deleted or added to the list) information to bound controls. Therefore, lists used for data binding must implement the IBindingList, which provides both types of change notification. The BindingList<T> is a generic implementation of IBindingList and is designed for use with Windows Forms data binding. You can create a BindingList<T> that contains a business object type that implements INotifyPropertyChanged and the list will automatically convert the PropertyChanged events to ListChanged events. If the bound list is not an IBindingList, you must bind the list of objects to Windows Forms controls by using the BindingSource component. The BindingSource component will provide property-to-list conversion similar to that of the BindingList<T>. For more information, see How to: Raise Change Notifications Using a BindingSource and the INotifyPropertyChanged Interface.

Change Notification for Custom Controls

Finally, from the control side you must expose a PropertyNameChanged event for each property designed to be bound to data. The changes to the control property are then propagated to the bound data source. For more information, see How to: Apply the PropertyNameChanged Pattern

See Also

Reference

BindingSource

INotifyPropertyChanged

BindingList<T>

Concepts

Data Sources Supported by Windows Forms

Data Binding and Windows Forms

Other Resources

Windows Forms Data Binding