Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does PropertyChangedEventHandler work?

This is a really simple question, but I was wondering if someone could explain what the 4th line is actually doing? so the first line gives an event to the handler. I don't really know in what circumstances handler will return null or what the last line does.

When you pass the handler your object and which property changed, what does it do with them?

PropertyChangedEventHandler handler = PropertyChanged; //property changed is the event

if (handler != null)
{
    handler(this, new PropertyChangedEventArgs(name));
}

I assume I used this to get this code but I would like to understand what it is doing fully.

like image 604
James Joshua Street Avatar asked Jul 19 '13 01:07

James Joshua Street


People also ask

What is the purpose of INotifyPropertyChanged?

The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed. For example, consider a Person object with a property called FirstName .

How do you implement INotifyPropertyChanged?

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.

What is OnPropertyChanged in WPF?

INotifyPropertyChanged is an interface used by binding sources (i.e. the DataContext) to let the user interface or other components know that a property has been changed. WPF automatically updates the UI for you when it sees the PropertyChanged event raised.


2 Answers

If you just did:

PropertyChanged(this, new PropertyChangedEventArgs(name))

you would get a NullReferenceException if no one was subscribed to the event PropertyChanged. To counteract this you add a null check:

if(PropertyChanged != null)
{
    PropertyChanged(this, new PropertyChangedEventArgs(name))
}

Now, if you are using multi-threading someone could unsubscribe between the null check and the calling of the event, so you could still get a NullReferenceException. To handle that we copy the event handler to a temporary variable

  PropertyChangedEventHandler handler = PropertyChanged;
  if (handler != null)
  {
    handler(this, new PropertyChangedEventArgs(name));
  }

Now if someone unsubscribes from the event our temporary variable handler will still point to the old function and this code now has no way of throwing a NullReferenceException.

Most often you will see people use the keyword var instead, this makes it so you don't need to type in the full type of the temporary variable, this is the form you will see most often in code.

  var handler = PropertyChanged;
  if (handler != null)
  {
    handler(this, new PropertyChangedEventArgs(name));
  }
like image 102
Scott Chamberlain Avatar answered Sep 23 '22 07:09

Scott Chamberlain


PropertyChanged is the event that was declared like this, according to its definition in the interface:

public event PropertyChangedEventHandler PropertyChanged;

Events that are defined like that are actually a syntactic sugar for a list of event handlers you can add a delegate (that is a reference to a function) to by subscribing, or remove a delegate by unsubscribing.

Now, when you call an event, i.e. PropertyChanged(...), then what happens internally is that every delegate in that internal list is called separately with the parameters. This will tell all the subscribers of your event that the event happened.

Now, the reason for the whole thing with the handler variable is, that PropertyChanged can be null. If nothing subscribed to it, then calling the event (or rather the event handler list) would not work, so this is just a way to ensure that you can actually execute the handler.

like image 20
poke Avatar answered Sep 24 '22 07:09

poke