1

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.

Adi Lester
  • 23,781
  • 12
  • 86
  • 106
Tomer W
  • 3,092
  • 1
  • 22
  • 38

2 Answers2

3

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.

Henk Holterman
  • 236,989
  • 28
  • 287
  • 464
  • Hard choice. An extra backing field bloats the object size and may hurt performance. Unecessary events lead to unecessary memory allocations, which can also hurt performance. – Jonathan Allen Jul 31 '12 at 07:32
  • 1
    @JonathanAllen - adding 1 `int` to a list should be the clear winner here. – Henk Holterman Jul 31 '12 at 07:36
1

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.

blindmeis
  • 21,084
  • 7
  • 47
  • 70
  • should note that `OnCollectionChanged` When `if ((action == remove && collection.count == 0) || (action == add && collection.count == 1)) Raise("IsSame")` – Tomer W Jul 31 '12 at 11:59