Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raise an event whenever a property's value changed?

There is a property, it's named ImageFullPath1

public string ImageFullPath1 {get; set; } 

I'm going fire an event whenever its value changed. I am aware of changing INotifyPropertyChanged, but I want to do it with events.

like image 588
Mohammad Dayyan Avatar asked Feb 11 '10 18:02

Mohammad Dayyan


People also ask

How do you raise an event?

Typically, to raise an event, you add a method that is marked as protected and virtual (in C#) or Protected and Overridable (in Visual Basic). Name this method On EventName; for example, OnDataReceived .

What is Raisepropertychanged?

The RaisePropertyChanging event is used to notify UI or bound elements that the data has changed. For example a TextBox needs to receive a notification when the underlying data changes, so that it can update the text you see in the UI.

How do you implement property change?

To implement INotifyPropertyChanged you need to declare the PropertyChanged event and create the OnPropertyChanged method. Then for each property you want change notifications for, you call OnPropertyChanged whenever the property is updated.

Why PropertyChanged is null?

Although you haven't shown us the XAML use of the property I would think that one way the PropertyChanged is null is when you set the value of a control (which is bound) in code, this will destroy any binding.


1 Answers

The INotifyPropertyChanged interface is implemented with events. The interface has just one member, PropertyChanged, which is an event that consumers can subscribe to.

The version that Richard posted is not safe. Here is how to safely implement this interface:

public class MyClass : INotifyPropertyChanged {     private string imageFullPath;      protected void OnPropertyChanged(PropertyChangedEventArgs e)     {         PropertyChangedEventHandler handler = PropertyChanged;         if (handler != null)             handler(this, e);     }      protected void OnPropertyChanged(string propertyName)     {         OnPropertyChanged(new PropertyChangedEventArgs(propertyName));     }      public string ImageFullPath     {         get { return imageFullPath; }         set         {             if (value != imageFullPath)             {                 imageFullPath = value;                 OnPropertyChanged("ImageFullPath");             }         }     }      public event PropertyChangedEventHandler PropertyChanged; } 

Note that this does the following things:

  • Abstracts the property-change notification methods so you can easily apply this to other properties;

  • Makes a copy of the PropertyChanged delegate before attempting to invoke it (failing to do this will create a race condition).

  • Correctly implements the INotifyPropertyChanged interface.

If you want to additionally create a notification for a specific property being changed, you can add the following code:

protected void OnImageFullPathChanged(EventArgs e) {     EventHandler handler = ImageFullPathChanged;     if (handler != null)         handler(this, e); }  public event EventHandler ImageFullPathChanged; 

Then add the line OnImageFullPathChanged(EventArgs.Empty) after the line OnPropertyChanged("ImageFullPath").

Since we have .Net 4.5 there exists the CallerMemberAttribute, which allows to get rid of the hard-coded string for the property name in the source code:

    protected void OnPropertyChanged(         [System.Runtime.CompilerServices.CallerMemberName] string propertyName = "")     {         OnPropertyChanged(new PropertyChangedEventArgs(propertyName));     }      public string ImageFullPath     {         get { return imageFullPath; }         set         {             if (value != imageFullPath)             {                 imageFullPath = value;                 OnPropertyChanged();             }         }     } 
like image 89
Aaronaught Avatar answered Sep 20 '22 14:09

Aaronaught