0

I am following the PluralSight tutorials on WPF and data binding. My code giving an error when trying to implement INotifyPropertyChanged.

public class Employee : INotifyPropertyChanged 
{
    public string Name { get; set; }
    public string Title { get; set; }

    public static Employee getEmployee()
    {
        var emp = new Employee() { Name = "Tom", Title = "Developer" };
        return emp;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged( [CallerMemberName] string caller = "")
    {
        if ( PropertyChanged != null)
        {

            PropertyChanged (this, 
                 new PropertyChangingEventArgs (caller));

        }
    }
}

The error is on the last statement (at the end of the if statement), saying:

"Cannot convert from System.ComponentModel.PropertyChangingEventArgs to System.ComponentModel.PropertyChangedEventArgs"

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Tom Peters
  • 31
  • 3
  • 1
    Possible duplicate of [Implementing INotifyPropertyChanged - does a better way exist?](http://stackoverflow.com/questions/1315621/implementing-inotifypropertychanged-does-a-better-way-exist) – Gusman May 11 '16 at 20:51
  • 1
    Why would you write out the error message instead of just copying it? – Jeroen Vannevel May 11 '16 at 20:54

2 Answers2

8

Error does not really say that, it says that it cannot convert from PropertyChangingEventArgs to PropertyChangedEventArgs. So use correct arguments class name:

public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string caller = "")
{
    if (PropertyChanged != null)
     {
        PropertyChanged(this,
                 new PropertyChangedEventArgs(caller));

    }
}
Evk
  • 98,527
  • 8
  • 141
  • 191
4

You are using the wrong event arg. PropertyChangingEventArgs when you should be using PropertyChangedEventArgs

private void OnPropertyChanged( [CallerMemberName] string caller = "")    
{
    if ( PropertyChanged != null)
    {    
        PropertyChanged (this, new PropertyChangedEventArgs (caller));    
    }    
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472