Question

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.

Was it helpful?

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.

OTHER TIPS

when ever you collection changes you should call OnPropertyChanged("IsSame"); - thats right. but when to call depends on your viewmodel logic.

edit: assume that you have an Add and Remove command, then you have to call OnPropertyChanged("IsSame"); within these methods.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top