2

I am currently away from home (and will be for a few more weeks) and only have a tablet - therefore, I have no access to Visual Studio to test what I'm trying to learn - the MVVM pattern.

So far, I think the theory is set, but I'm confused about the INotifyPropertyChanged interface.

I think one of the ideas of the MVVM pattern is to be able to update the Model which in turn notifies the ViewModel which in turn notifies the view. If I'm wrong then the rest of this question is meaningless.

My question is, do all the classes have to share 1 implementation of INotifyPropertyChanged?

In other words, which is correct:

  1. A property name is the same for all classes (but each class has it's own unique implementation of INotifyPropertyChanged)

  2. A property name is the same for all classes (and each class inherits from a single base class which implements INotifyPropertyChanged)?

MyDaftQuestions
  • 4,487
  • 17
  • 63
  • 120

1 Answers1

4

No, they don't have to share anything. However the implementation of INotifyPropertyChanged is some lines of code, so we usually made a base class for models - like BaseModel or BaseViewModel which implemented INotifyPropertyChanged.

The implementations are generally specific to the version of C# language you use (the older ones works in the newer version of language).

Look here: http://jesseliberty.com/2012/06/28/c-5making-inotifypropertychanged-easier/

but instead of having the Employee class to implement the INotifyPropertyChanged, you can implement it in a base class

public class BaseViewModel : INotifyPropertyChanged
{

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

and then the Employee class from the blog should look like this

public class Employee : BaseViewModel
{
    private string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            RaisePropertyChanged();
        }
    }
}

this is for C# 5 (the .Net 4.5 - the one which does not run on Windows XP)

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
eMko
  • 1,147
  • 9
  • 22
  • So, the important thing is the PropertyName must match? If I do `RaisePropertyChanged("MyName")` then this will notify every property called MyName... I guess this is also a potential problem if you're not careful with naming – MyDaftQuestions Apr 29 '14 at 08:35
  • Yes, exactly. Therefore in C# 5 it's solved via the [CallerMemberName] so you don't have to think about it during refactoring. (If I remember correctly, the ReSharper refactored it correctly, but the default Visual Studio refactorings did not.) – eMko Apr 29 '14 at 08:39
  • For creating the properties it's generally a good practice to create the code template (or Live Template from Resharper) so you don't mess the names - the template will name it correctly for you. – eMko Apr 29 '14 at 08:40
  • I have reshaprer back at home... Any way, the code you've given, where you don't specify the PropertyName. Is that valid from .NET 4.5 or from .NET 4.0? I can't find it online :( – MyDaftQuestions Apr 29 '14 at 09:01
  • it's valid for .Net version 4.5 and C# language version 5 (that one with async/await etc). For .Net version 4 (and C# language version 4) it looks like this: http://www.dreamincode.net/forums/topic/208833-using-the-inotifypropertychanged-functionality/ – eMko Apr 29 '14 at 13:03