44

I'm not well versed in event-based programming. Basically, I'm still stumbling around with it. I'm trying to get something set up, but even with the tutorials, I can't wrap my head around it. What I would like to do (in words) is the following:

  1. I have a dataobject where a property changes. I notice this in the setter of the property, and want to raise an event that the property has changed.

  2. Elsewhere (in a different class entirely), I want to know that the property on this object has changed, and take some action.

Now I'm sure this is a common enough scenario, but my google-fu is letting me down. I'm simply not understanding http://msdn.microsoft.com/en-us/library/ms743695.aspx.

I have this:

public class ChattyClass {
  private int someMember;

  public event PropertyChangedEventHandler PropertyChanged;

  public int SomeMember {
    get {
      return this.someMember;
    }
    set {
      if (this.someMember != value){
        someMember = value;
        // Raise event/fire handlers. But how?
      }
   }
}

public class NosyClass{
  private List<ChattyClass> myChatters;

  public void addChatter(ChattyClass chatter){
    myChatters.add(chatter);
    // Start listening to property changed events
  }

  private void listner(){
    // I want this to be called when the PropertyChangedEvent is called
    Console.WriteLine("Hey! Hey! Listen! A property of a chatter in my list has changed!");
  }
}

What do I do to wire this up?

Concerning the comment pointing me back to the link:

In the example I see:

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

What I'm not understanding:

  • Why isn't this just calling PropertyChanged(this, new PropertyCHangedEventArgs(name))
  • Where does PropertyChanged get assigned?
  • What does the assignment look like?
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Martijn
  • 11,964
  • 12
  • 50
  • 96
  • The example you link to shows the necessary `OnPropertyChanged` implementation; can you be clearer where the gap is here? – Marc Gravell Aug 20 '12 at 09:06
  • 1
    Your class needs to implement the INotifyPropertyChanged interface to make it work. So the first line should be: public class ChattyClass : INotifyPropertyChanged { – Chaim Zonnenberg Aug 20 '12 at 09:07
  • @C.Zonnenberg there is no need to implement `INotifyPropertyChanged`. One can just add the `PropertyChanged` event to any class if he doesn't want to use it in any kind of data-binding. But anyway it's a best practice to use the mentioned interface. – nemesv Aug 20 '12 at 09:11
  • 1
    In WPF/Silverlight it is necessary. Refer to http://stackoverflow.com/questions/1644080/why-does-onpropertychanged-not-work-in-code-behind – Chaim Zonnenberg Aug 20 '12 at 09:13
  • The link is (effectively) broken. – Peter Mortensen May 10 '16 at 21:23

4 Answers4

48

You have to fire the event. In the example on MSDN, they made a protected method OnPropertyChanged to handle this easier (and to avoid duplicate code).

// Create the OnPropertyChanged method to raise the event 
protected void OnPropertyChanged(string name)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(name));
    }
}

What this method does, is look whether there is an event handler assigned or not (if it is not assigned and you just call it, you'll get a NullReferenceException). If there is one assigned, call this event handler. The event handler provided, has to have the signature of the PropertyChangedEventHandler delegate. This signature is:

void MyMethod(object sender, PropertyChangedEventArgs e)

Where the first parameter has to be of the type object and represents the object that fires the event, and the second parameter contains the arguments of this event. In this case, your own class fires the event and thus give this as parameter sender. The second parameter contains the name of the property that has changed.

Now to be able to react upon the firing of the event, you have to assign an event handler to the class. In this case, you'll have to assign this in your addChatter method. Apart from that, you'll have to first define your handler. In your NosyClass you'll have to add a method to do this, for example:

private void chatter_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    Console.WriteLine("A property has changed: " + e.PropertyName);
}

As you can see, this method corresponds to the signature I explained before. In the second parameter, you'll be able to find the information of which parameter has been changed. Last thing to do, is add the event handler. Now in your addChatter method, you'll have to assign this:

public void AddChatter(ChattyClass chatter)
{
    myChatters.Add(chatter);
    // Assign the event handler
    chatter.PropertyChanged += new PropertyChangedEventHandler(chatter_PropertyChanged);
}

I would suggest you to read something about events in .NET / C#: http://msdn.microsoft.com/en-us/library/awbftdfh . I think after reading/learning this, things will be more clear to you.

You can find a console application here on pastebin if you would like to test it quickly (just copy/paste into a new console application).

With newer versions of C#, you can inline the call to the event handler:

// inside your setter
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(MyProperty)));

You could also use something like Fody PropertyChanged to automatically generated the necessary code (visit the link to their GitHub page, with samples).

Styxxy
  • 7,462
  • 3
  • 40
  • 45
  • 1
    Thanks a ton, I'm almost there. What I don't understand yet is why I need a `chatter.PropertyChanged += new PropertyChangedEventHandler(chatter_PropertyChanged);` rather than just `chatter.PropertyChanged += chatter_PropertyChanged` – Martijn Aug 20 '12 at 09:32
  • It also works, however, the one I posted is what Visual Studio automatically inserts (the moment you do `+=`, VS will suggest you to auto generate the event handler and if you press tab, it'll insert that code). – Styxxy Aug 20 '12 at 09:34
  • The reason of this can be found on the MSDN pages. "Note that the previous syntax is new in C# 2.0. It is exactly equivalent to the C# 1.0 syntax in which the encapsulating delegate must be explicitly created by using the new keyword." (source: http://msdn.microsoft.com/en-us/library/ms366768) – Styxxy Aug 20 '12 at 09:38
  • 2
    Ok, I know the name of the property, but how can I get its value? – Pedro77 Oct 07 '13 at 17:33
  • 2
    You did something that 100 pages of Microsoft documentation are unable to do; that is explained how to use basic and fundamental functionality with a simple well explained example. – ejectamenta Aug 17 '23 at 15:44
14

The link that you looked is for the MVVM pattern and WPF. It is not a general C# implementation. You need something like this:

public event EventHandler PropertyChanged;

    public int SomeMember {
        get {
            return this.someMember;
        }
        set {
            if (this.someMember != value) {
                someMember = value;
                if (PropertyChanged != null) { // If someone subscribed to the event
                    PropertyChanged(this, EventArgs.Empty); // Raise the event
                }
            }
        }

...

public void addChatter(ChattyClass chatter) {
    myChatters.add(chatter);
    chatter.PropertyChanged += listner; // Subscribe to the event
}
// This will be called on property changed
private void listner(object sender, EventArgs e){
    Console.WriteLine("Hey! Hey! Listen! A property of a chatter in my list has changed!");
}

If you want to know what property has changed you need to change your event definition to:

public event PropertyChangedEventHandler PropertyChanged;

And change the calling to:

public int SomeMember {
    get {
        return this.someMember;
    }
    set {
        if (this.someMember != value){
            someMember = value;
            if (PropertyChanged != null) { // If someone subscribed to the event
                PropertyChanged(this, new PropertyChangedEventArgs("SomeMember")); // Raise the event
            }
        }
   }

   private void listner(object sender, PropertyChangedEventArgs e) {
       string propertyName = e.PropertyName;
       Console.WriteLine(String.Format("Hey! Hey! Listen! a {0} of a chatter in my list has changed!", propertyName));
   }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vale
  • 3,258
  • 2
  • 27
  • 43
  • 1
    You don't want to use `EventArgs.Empty` for the `PropertyChanged` event. The whole point is the listener is aware of the name of the property that changed. – ken2k Aug 20 '12 at 09:24
  • @ken2k I didn't implement "real" PropertyChanged event here. I just showed him how to implement any event himself. For PropertyChanged and INotifyPropertyChanged interface it would, of course be different. – Vale Aug 20 '12 at 09:26
  • Thank you. The reason I chose to accept Styxxy's answer over yours is the explanation on the signature of the delegate. It was basically a coin flip; it felt like a relief to read your "The link that you looked is for MVVM pattern and WPF, it is not general c# implementation." It is in fact half an implementation there, it does nothing by itself: there is no handling code ther. I'm not going crazy. – Martijn Aug 20 '12 at 09:42
6

why isn't this just calling PropertyChanged(this, new PropertyCHangedEventArgs(name))

Because if no one attached an handler to the event, then the PropertyChanged object returns null. So you'll have to ensure it's not null before calling it.

where does PropertyChanged get assigned?

In the "listener" classes.

For example, you could write in other class:

ChattyClass tmp = new ChattyClass();
tmp.PropertyChanged += (sender, e) =>
    {
        Console.WriteLine(string.Format("Property {0} has been updated", e.PropertyName));
    };

What does the assignment look like?

In C# we use the assignment operators += and -= for events. I recommend reading the following article to understand how to write event handlers using the anonymous method form (example above) and the "old" form.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ken2k
  • 48,145
  • 10
  • 116
  • 176
4

From taking the original code, and incorporating @Styxxy 's answer, I come out with:

public class ChattyClass  : INotifyPropertyChanged 
{
  private int someMember, otherMember;

  public int SomeMember
  {
      get
      {
          return this.someMember;
      }
      set
      {
          if (this.someMember != value)
          {
              someMember = value;
              OnPropertyChanged("Some Member");
          }
      }
  }

  public int OtherMember
  {
      get
      {
          return this.otherMember;
      }
      set
      {
          if (this.otherMember != value)
          {
              otherMember = value;
              OnPropertyChanged("Other Member");
          }
      }
  }

  protected virtual void OnPropertyChanged(string propertyName)
  {
      PropertyChangedEventHandler handler = PropertyChanged;
      if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
  }

  public event PropertyChangedEventHandler PropertyChanged;

}

public class NosyClass
{
    private List<ChattyClass> myChatters = new List<ChattyClass>();

    public void AddChatter(ChattyClass chatter)
    {
        myChatters.Add(chatter);
        chatter.PropertyChanged+=chatter_PropertyChanged;
    }

    private void chatter_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        Console.WriteLine("A property has changed: " + e.PropertyName);
    }
}
Paul Richards
  • 1,181
  • 1
  • 10
  • 29
  • everything in this example seems to have a backing (aka local) variable to hold the value. The example I am looking for does not require that. – George M Ceaser Jr Jan 24 '17 at 20:10
  • The name of the property is `OtherMember` but the Event is called with `Other Member` (with space). Same for SomeMember. May this causes wrong behavior. – KargWare Dec 02 '22 at 07:27