Jack February 2016

Raising OnPropertyChanged in the setter of each property vs Instance of Object

Information for the question:

I am trying to understand how to properly implement INotifyPropertyChanged on objects and collections.

First, here is my ViewModelBase class:

public abstract class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertychanged([CallerMemberName] string propertyName = "")
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Consider that I have a class called Person:

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Age { get; set; }
}

To use INotifyPropertyChanged, most examples that I have seen change the Person class to something like this:

public class Person
{
    public int Id { get; set; }

    private string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            OnPropertychanged();
        }
    }

    private string _age;
    public string Age
    {
        get { return _age; }
        set
        {
            _age = value;
            OnPropertychanged();
        }
    }
}

It seems to work exactly the same when used a single time on an instance of the object (This might be useful if there are a lot of properties):

private Person _person;
public Person MyPerson
{
    get { return _person; }
    set
    {
        _person = value;
        OnPropertychanged();
    }
}

Actual question:

1 - Does it make a difference (aside from amounts of code) whether you call OnPropertychanged() on each individual property verses on an instance of an object? (Are both cons

Answers


Filip February 2016

1) Well, if you want to call it on object instance, then you need to do it every time you use your class like this in binding. When you implement OnNotifyPropertyChanged directly inside your class, you don't need to care about it later on...

2) Classes with INotifyPropertyChanged do not require Observable collections. This is however must when you are binding colection do some UI control (ListBox, ListView) and want to add/remove its elements. Observable collection will then make sure the UI gets updated.


Edd February 2016

The ObservableCollections object... When adding and removing from this collection the UI will be notified of the changes (Top Level). If you have an "ObservableCollection of Person" and you change a property on the one of the objects(Person) in the list the UI will not update unless your "Person" class implements the INotifyPropertyChanged interface, which can be put into a base class that all classes can inherit from like your example. I hope this helps a little.

Post Status

Asked in February 2016
Viewed 2,099 times
Voted 7
Answered 2 times

Search




Leave an answer