8

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.

WiiMaxx
  • 5,322
  • 8
  • 51
  • 89

2 Answers2

10

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.

Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
  • 3
    sure but the `ObservableCollection<>` is already in an `ViewModel` so it is already separated from the `View` or do i miss something? – WiiMaxx May 15 '13 at 08:37
  • 1
    @WiiMaxx It's not really separated, as you're still actually using the class in the View. – Mathew Thompson May 15 '13 at 08:43
  • Oh good objection. So you thing it will not be an overhead to create 2 objects Model-> ViewModel for a object that maybe never used from the user – WiiMaxx May 15 '13 at 08:51
  • If it's being served to the view, then I think it's necessary overhead (to adhere to the MVVM pattern). You could always reuse all if not parts of the View Model in other areas. – Mathew Thompson May 15 '13 at 09:00
0

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;}
}
cvraman
  • 1,687
  • 1
  • 12
  • 18
  • like mattytommo already says the `View` has to be separated from the Model so it would be more like select a `Model` create an `ViewModel`, give him the Model and set this `ViewModel` as `DataContext` – WiiMaxx May 15 '13 at 08:42
  • I would still go ahead with the above implementation since it would mean that we need to implement INotifyPropertyChanged in the Model class. This would make it easier to bind to the view. Take a look at this thread : http://stackoverflow.com/questions/6922130/in-mvvm-model-should-the-model-implement-inotifypropertychanged-interface – cvraman May 15 '13 at 08:47