Home > DeveloperSection > Forums > How raise an event whenever a property's value changed?
Manoj Bhatt
Manoj Bhatt

Total Post:153

Points:1079
Posted on    January-04-2016 5:27 AM

 C# C#  .NET 
Ratings:


 3 Reply(s)
 253  View(s)
Rate this:
I want to use an event whenever a property's value changed . how to use this in a program please help me.


Anupam Mishra

Total Post:135

Points:949
Supported
Posted on    January-04-2016 5:36 AM

In C#, the INotifyPropertyChanged interface is used notify clients, typically binding clients, that a property value has changed.

For example, consider an Employee class object with a property called FirstName. To provide property-change notification, the Employee class type implements the INotifyPropertyChanged interface and raises a PropertyChanged event when FirstName is changed.

For change notification to occur in a binding between a bound client and a data source, your bound type should either:

·         Implement the INotifyPropertyChanged interface (preferred).

·         Provide a change event for each property of the bound type.

Do not do both.

For example, in below we have defined a property ‘FirstName

private string _firstName;
        public string FirstName
        {
            get
            {
                return _firstName;
            }
            set
            {
                _firstName = value;
                OnPropertyChanged("FirstName");
            }
        }

Now, we have implementing the INotifyPropertyChanged interface

 

public event PropertyChangedEventHandler PropertyChanged;
 
        protected void OnPropertyChanged(string propertyName)
        {
            OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }
 
 // occurring event        private void OnPropertyChanged(PropertyChangedEventArgs e)
        {
                     MessageBox.Show(“Changed in First Name ”+FirstName);
        }

Output:


I think in above example you are understand about raises event when 'FirstName'  property values are changed.

    For more details , you click here


Modified On Jan-04-2016 05:37:49 AM

Manoj Bhatt
Manoj Bhatt

Total Post:153

Points:1079
Posted on    January-05-2016 6:35 AM

thank you sir.

Anupam Mishra

Total Post:135

Points:949
Posted on    January-06-2016 12:01 AM

For more details you can also read out this one: Click Here

Modified On Jan-06-2016 12:03:30 AM

Don't want to miss updates? Please click the below button!

Follow MindStick