Search code examples
c#silverlightmvvmwindows-phone-7

Difference between Model and ViewModel


I've never used MVVM before, so I'm probably missing something obvious. When I create a new Panorama application, there's already a ViewModel folder containing ItemViewModel and MainViewModel.

I thought "MainViewModel.cs" is the file that organizes the panorama. However, within MainViewModel, it has this line:

public MainViewModel()
{
    this.Items = new ObservableCollection<ItemViewModel>();
}

The ItemViewModel has no interaction with the panorama. These are the then instantiated like this:

this.Items.Add(new ItemViewModel() 
{ 
    LineOne = "first line", 
    LineTwo = "second line", 
    LineThree = "third line" 
});

Why isn't ItemViewModel just a 'Model'? It implements INotifyPropertyChanged, but for what purpose? I would've thought that the ObservableCollection in MainViewModel would be enough to notify any changes, as demonstrated here


Solution

  • The ObservableCollection will notify when items are added or deleted from the list, but the INotifyPropertyChanged on the ItemViewModel is needed if you want notifications to happen when those properties change.