locked
MVVM: using ObservableCollection<Customer> OR ObservableCollection<CustomerViewModel> ? RRS feed

  • Question

  • Hello,

    The ObservableCollection is a special WPF collection that provides proper notifications to the UI when items are added, removed, or the list is refreshed because it implements INotifyCollectionChanged . It’s common to use this collection (or inherit from it) to contain your business objects you want to bind to in WPF.


    Thats fine for the first but in www I see people using the ObservableCollection in MVVM tutorials/samples in different ways. What is the advantage/disadvantage of using a ObservableCollection with Customer or CustomerViewModel as its containing data ?
    Sunday, October 4, 2009 5:33 PM

Answers

  • Hi Lisa,

    Whether using CustomerViewModel or Customer itself as containing data depends on the design of your WPF application.

    Normally, MVVM recommends setting the DataContext property of a Window(i.e. a view) to a ViewModel object so that you can bind to the properties in the ViewModel from the view conveniently. You can expose a property of type ObservableCollection<Customer> from the ViewModel class.

    Hope this helps.

    Sincerely,
    Linda Liu


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Linda Liu Tuesday, October 13, 2009 2:50 AM
    Monday, October 12, 2009 10:55 AM

All replies

  • MyProblem is that you might want to make an intermediat layer if you want to sort or filter. Using ListCoillectionView
    Kenneth
    Sunday, October 4, 2009 5:55 PM
  • I do not want to sort or filter. I just want to bind a collection accessed in the ViewModel to the View datacontext. My question was concerning many www samples either using

    CustomerViewModel or Customer itself as containing data. Where is the advantage/disadvantage or the purpose to favor one before the other?
    Sunday, October 4, 2009 8:24 PM
  • Hi Lisa,

    Whether using CustomerViewModel or Customer itself as containing data depends on the design of your WPF application.

    Normally, MVVM recommends setting the DataContext property of a Window(i.e. a view) to a ViewModel object so that you can bind to the properties in the ViewModel from the view conveniently. You can expose a property of type ObservableCollection<Customer> from the ViewModel class.

    Hope this helps.

    Sincerely,
    Linda Liu


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Linda Liu Tuesday, October 13, 2009 2:50 AM
    Monday, October 12, 2009 10:55 AM
  • If you want to sort or filter, you can use the CollectionViewSource. Take a look at Josh Smith's  in MSDN Magazine (http://msdn.microsoft.com/en-us/magazine/dd419663.aspx) and download the demo code. The AllCustomersView control uses the CollectionViewSource to provide those features.
    Wednesday, December 16, 2009 2:47 PM
  • thanks I know this article nearly in and out , read it 9 months ago...
    Friday, December 18, 2009 11:53 AM