This project has moved and is read-only. For the latest updates, please go here.

What about a generic CollectionViewModel which wraps a model collection

Mar 30, 2011 at 2:40 PM
Edited Mar 31, 2011 at 6:12 AM

Hi Laurent

first oft all thanks for the very nice light MVVM framework. But there is a thing I am missing very much: a generic CollectionViewModel which wraps a collection of model objects, wraps each model object in a ViewModel and keeps the collection of model and the collection of ViewModel objects in sync.

I am really interested in your opinion since there are so many discussions in the web.

Greetings from Germany,


Apr 1, 2011 at 6:57 AM
Edited Apr 1, 2011 at 6:57 AM

Hi Laurent,

me again :-)

I think a generic CollectionViewModel is a really interesting thing. You do not agree with me? I have read many many discussions in the web where folks wrote their own solutions. But every implementation has the one or other disadvantage so I thought an implementation in a framework could be much better because many people are giving their input and testing takes place a lot by the community. You know, many eyes are better than one.

Here are my first steps and I will go on with it in the next few days:

public interface IContainsModel<TModel>
  TModel Model { get; set; }

public class CollectionItemViewModel<TModel> : ViewModelBaseDynamic, IContainsModel<TModel>

  #region IContainsModel<TModel> Members

  public TModel Model { get; set; }


public class CollectionViewModel<TModel, TViewModel> : ObservableCollection<TViewModel>
	where TViewModel : IContainsModel<TModel>, new()
  private IEnumerable<TModel> _models;
  private ObservableCollection<TViewModel> _viewModels;

  public CollectionViewModel(IEnumerable<TModel> modelCollection)
    _models = modelCollection;
    _viewModels.AddRange(modelCollection.Select(model => new TViewModel() { Model = model }));

    INotifyCollectionChanged modelCollectionAsINotifyCollectionChanged = modelCollection as INotifyCollectionChanged;
    if (modelCollectionAsINotifyCollectionChanged != null)
      // Handle the synchronization from model collection to ViewModel collection
      //modelCollectionAsINotifyCollectionChanged.CollectionChanged += ...

    // Handle the synchronization from ViewModel collection to model collection
    //_viewModels.CollectionChanged += ...

By the way: ViewModelBaseDynamic is a class which derives from DynamicObject and otherwise has the same implementation than your ViewModelBase. I will write an interface to enable the CollectionViewModel to support both of them.

What are you thinking about that?


Apr 5, 2011 at 12:46 PM


First, thanks a lot for taking the time to write. I am not dissing any idea, I do realize that some are very useful to certains while they seem less so to others. My goal in MVVM Light is to keep it, well, light ;) by adding only features that are a) not very easy to add by yourself, b) are causing a lot of repetitive work if done without a helper or c) all of the above.

In the case of this particular feature, it is not something that I have felt a strong need for. Now I know that you use that, and I know that for you, this feature is very useful. I did not however observe a need for it in the community in general. My recommendation would be to publish that in your blog, and see what the response is. If you can show me that there is a strong demand for this feature, I will be happy to reconsider and maybe add it in V5.




Apr 14, 2011 at 7:57 AM

Hi Laurent,

hold on tight: I don't have a blog! I think blogs are great if you have something to say more than once a half year. There are toooooo many blogs out there which contains only a handful of useful posts. Don't you lose track of blogs you are interested in?

Back to subject! My colleagues and me are wondering over and over again about the omission of "to many" relationships in the MVVM pattern. Many articels in magazines and blogs about MVVM don't consider them. So I was really wondering about the missing support for them in the existing MVVM frameworks/tools/helpers.

My suggestion is to post my code here in a few days in this discussion and announce it on stackoverflow so we can see if there is a demand for such a feature. Alright? I am looking forward for the upcoming discussions.

Greetings, Tobias

May 4, 2011 at 4:54 AM

I posted an ObservableCollection and a BindingList at my blog. Try those out and see if they meet your needs.

Laurent, if I see a good response, you can have these classes for V5. I'll let you know.

May 4, 2011 at 1:00 PM

Good feedback guys, thanks. I will keep an eye on that matter.