Search code examples
c#wpfmvvminotifypropertychangedinotifycollectionchanged

Implementing INotfyPropertyChanged for calculated properties


I'm a bit new to MVVM, and was wondering

Let's say I have defined a ObservableCollection<Differences> Diffs property. I also have the following property:

public bool IsSame
{
    get
    {
         return Diffs.Count == 0;
    }
 }

I dont get how I'm supposed to implement the OnPropertyChanged for IsSame, because it is implicit from the Diff list.

  • Should I attach to the List OnCollectionChanged event and then check if it changes IsSame ?
  • Should I use a backing field anyway and handle the List OnCollectionChanged?

Thank you very much.


Solution

  • Should I use a backing field anyway and handle the List OnCollectionChanged?

    To do it correctly: Yes.

    When related properties change it's up to the source to raise all events. Your main problem here is to detect when IsSame actually changes (ie goes from 1 to 0 or from 0 to 1). You need either a backing field or you will raise the event (much) more often then is required.