Example for Property Change Notification in C#.NET


When a property is binded to UI element, and if the property value changes , if you want to reflect the changed value on the binded UI element, you have make sure the property containing class should implement INotifyPropertyChanged inteface.

In the below example, TextBox text property is binded with "Grade" property of Student Class.
if Student Class , Grade property is changed to new value it should reflect on the grade  textbox of the below figure


.NET Property Change Notification C#

using System;

using System.ComponentModel;

using System.Windows.Forms;

 

namespace PropertyChangeNotification

{

 

    //Student class implements  INotifyPropertyChanged interface

    class Student : INotifyPropertyChanged

    {

 

        private string name;

 

        public string Name

        {

            get { return name; }

            set { name = value; }

        }

        private string grade;

 

        public string Grade

        {

            get { return grade; }

            set

            {

                if (value != Grade)

                {

                    grade = value;

                    //Whenever property value is changes

                    //PropertyChanged event is triggered

                    OnPropertyChanged("Grade");

                }

            }

        }

 

 

        #region INotifyPropertyChanged Members

 

        public event PropertyChangedEventHandler PropertyChanged;

 

        //Create OnPropertyChanged method to raise event

        protected void OnPropertyChanged(string PropertyName)

        {

            if (PropertyChanged != null)

                PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));

        }

 

        #endregion

    }

 

 

    //Form Class

    public partial class Form1 : Form

    {

        //Create instanct to bind

        Student MyStudent = new Student { Name = "John", Grade = "B" };

 

        public Form1()

        {

            InitializeComponent();

 

            tbName.Text = MyStudent.Name;

 

            //Binding the Textbox text property and student class Grade property

            tbGrade.DataBindings.Add(new Binding("Text", MyStudent, "Grade"));

        }

 

        private void btnUpdateGrade_Click(object sender, EventArgs e)

        {

            //When ever property values is changes, property change notificaiton is triggered and

            //new value will be updated on to Textbox.

            MyStudent.Grade = "D";

        }

 

    }

 

}



Download the code snippet below
Example for Property Change Notification in C#

Zooming in Winforms Webbrowser Control in .NET
Filtering Datatable with Select Conditions and Sorting in C#.NET
Hiding a property from displaying in Datagridview in C#.NET
Logic to detect if Application is started from Citrix/Terminal Server/PC in C#
Winforms Application.DoEvents Method
Modify XML file with tagname in c#.net
Read Machine IP Address in C#
Check if IIS is running using C#
Implementing IEnumerable in C#
Built in Delegates in .NET Framework
Changing Winform webbrowser control IE version in C#.NET
Set the dropdown width of any combobox based on the data
Difference between Const and ReadOnly
Example Program using Delegates and Events in C#
AutoComplete in .NET Winforms TextBox
How to read data from XML String and insert in to table in SQL Server
WebBrowser.navigate Data Submission by Get or Post Methods
Datagridview Paging
Copy Datagridview cell value using ContextMenu
Inserting data in Datatable in C#
Cookie in .NET
Using Control's Child Index and Dock property for achieving desired layout in a form.
Accessing section group inside configuration of web.config
Create File in .NET C#
Change button shape in .net
Dispose vs Finalize in .net




INotifyPropertyChanged , Property Change Notification in C#.NET, Notify Property change in .net, PropertyChanged, Example for Property Change Notification in .NET