Question

public class ViewModel : INotifyPropertyChanged
{
    private string name;
    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            if (name != value)
            {
                name = value;
                OnPropertyChanged("Name");

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

public partial class MainWindow : Window
{
    private ViewModel vm;
    public MainWindow()
    {
        InitializeComponent();
        vm = new ViewModel { Name = "Shahrukh Khan" };
        DataContext = vm;
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        vm.Name = "Salman Khan";
    }
}

When I run following code I get this error:

INotifyUnderstandingYoutube.ViewModel does not implement interface member System.ComponentModel.INotifyPropertyChanged.PropertyChanged

I dont understand the reason behind this? Can anyone please explain what is the error?

Was it helpful?

Solution

Your

public PropertyChangedEventHandler PropertyChanged;

defines PropertyChanged as a field, which happens to be a field of delegate type, but a field nonetheless. INotifyPropertyChanged expectes an event.

public event PropertyChangedEventHandler PropertyChanged;
       ^^^^^

OTHER TIPS

It means you don't fully implement interface. On the interface declaration (after : INotifyPropertyChanged), press Shift+Alt+F10, and let the Visual Studio add code for you. You will then see what you were missing.

Seems like you are missing this piece of code in your ViewModel class:

event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged
{
    add { throw new NotImplementedException(); }
    remove { throw new NotImplementedException(); }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top