3.1 KiB
title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | ms.date | dev_langs | helpviewer_keywords | ms.assetid | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| How to: Raise Change Notifications Using a BindingSource and the INotifyPropertyChanged Interface | 03/30/2017 |
|
|
7fa2cf51-c09f-4375-adf0-e36c5617f099 |
How to: Raise Change Notifications Using a BindingSource and the INotifyPropertyChanged Interface
The xref:System.Windows.Forms.BindingSource component automatically detects changes in a data source when the type contained in the data source implements xref:System.ComponentModel.INotifyPropertyChanged and raises xref:System.ComponentModel.INotifyPropertyChanged.PropertyChanged events when a property value is changed. This change detection is useful because controls bound to the xref:System.Windows.Forms.BindingSource automatically update as the data source values change.
Note
If your data source implements xref:System.ComponentModel.INotifyPropertyChanged and you are performing asynchronous operations, you should not make changes to the data source on a background thread. Instead, you should read the data on a background thread and merge the data into a list on the UI thread.
Example
The following code example demonstrates a simple implementation of the xref:System.ComponentModel.INotifyPropertyChanged interface. It also shows how the xref:System.Windows.Forms.BindingSource automatically passes a data source change to a bound control when the xref:System.Windows.Forms.BindingSource is bound to a list of the xref:System.ComponentModel.INotifyPropertyChanged type.
If you use the CallerMemberName attribute, calls to the NotifyPropertyChanged method don't have to specify the property name as a string argument. For more information, see Caller Information (C#) or Caller Information (Visual Basic).
[!code-csharpSystem.ComponentModel.IPropertyChangeExample#1] [!code-vbSystem.ComponentModel.IPropertyChangeExample#1]
Compiling the Code
This example requires:
- References to the System, System.Data, System.Drawing, and System.Windows.Forms assemblies.