Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVVM: CollectionView in ViewModel or CollectionViewSource in xaml?

I'm developing a WPF application using the MVVM pattern and I need to display a list of items in a ListView (with filtering), with the fields of the selected item displayed in a Master/Detail view. I'm torn between the following two ways of doing this:

  1. Exposing a CollectionView in my ViewModel, and binding to this.
  2. Exposing a plain IList in my ViewModel, and using CollectionViewSource to create the CollectionView in XAML.

Is there an accepted way of doing this? Any thoughts on the best way?

like image 781
Grokys Avatar asked Jun 11 '09 04:06

Grokys


2 Answers

I do the former (expose CollectionView from the VM) but what really matters is where your filtering logic is. That should definitely be in the VM so that it can be tested, even if the view has to wire up the CollectionViewSource to the VM. That said, I don't think there's anything particularly nasty or anti-MVVM about exposing a CollectionView from your VM.

like image 120
Kent Boogaart Avatar answered Sep 22 '22 15:09

Kent Boogaart


I know I'm a bit late answering your question but I just saw it today.

I have created master/detail viewmodels that use the CollectionViewSource and blogged about it.

I wrote about a viewmodel for master/detail tables here that uses : http://reyntjes.blogspot.com/2009/04/master-detail-viewmodel_24.html

You can also find a viewmodel for the observablecollection class on my blog pages.

Maybe you find it of use to you.

like image 43
Robert Avatar answered Sep 23 '22 15:09

Robert