WiiMaxx
WiiMaxx

Reputation: 5420

Best Practice Observablecollection<Model> vs Observablecollection<ViewModel>

Which one is the better solution to hold my data or does it depends on some conditions?

sample situation 1:
you need to display a list of data which can be modified in an new window after selection.

sample situation 2:
you need to display a list of data which can be modified in this list.

Upvotes: 8

Views: 2658

Answers (2)

cvraman
cvraman

Reputation: 1697

I would say go with ObservableCollection<Model> since it is something that you can bind directly to the List or datagrid.

For Sample Situation 1 : Select a Model and then set the data context of the new Window to that Model.

For Sample Situation 2 : In place editing of the datagrid with 2 way binding.

The ObservableCollection can be inside a ViewModel. Something like the one shown below.

public class MyViewModel
{
     public ObservableCollection<Model> ListOfItems { get; set;}
}

Upvotes: 0

Mathew Thompson
Mathew Thompson

Reputation: 56459

As you're using MVVM, you should be going with ObservableCollection<ViewModel>.

The Model should be separated from the View by means of the ViewModel.

Upvotes: 10

Related Questions