In C#/WPF, what is the best way to provide a class which implements IObservableCollection<ViewModel<T>> and wraps around ObservableCollection<T> from the model?
The model can add/remove T elements to the underlying collection, or the view could add/remove ViewModel elements from the view-model layer.
Is there anything fishy about this from the standpoint of the MVVM pattern? Are there libraries containing this, or a similar class?
I wrote a class below that you can use for single threaded purposes. I've only implemented Add and Remove, so you'd have to implement any other actions that you may use:...
Usage:
var viewModelWrapper = new ObservableCollectionViewModel<ItemViewModel, Model>(Model.List, item => new ItemViewModel(item));
Generic class - observable collection view model wrapper:
public class ObservableCollectionViewModel<TItemViewModel, TModel> :
ObservableCollection<TItemViewModel>, IDisposable
where TItemViewModel : BaseViewModel<TModel>
where TModel : new()
{
private readonly ObservableCollection<TModel> models;
private readonly Func<TModel, TItemViewModel> viewModelConstructor;
public ObservableCollectionViewModel(ObservableCollection<TModel> models,
Func<TModel, TItemViewModel> viewModelConstructor)
{
this.models = models;
this.viewModelConstructor = viewModelConstructor;
CreateViewModelCollection();
models.CollectionChanged += models_CollectionChanged;
CollectionChanged += viewModels_CollectionChanged;
}
private void viewModels_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
models.CollectionChanged -= models_CollectionChanged;
try
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
{
foreach (var newItem in e.NewItems)
{
models.Add(((TItemViewModel)newItem).Model);
}
break;
}
case NotifyCollectionChangedAction.Remove:
{
foreach (var oldItem in e.OldItems)
{
models.Remove(((TItemViewModel)oldItem).Model);
}
break;
}
// TODO: Add missing actions
default: throw new NotImplementedException();
}
}
finally
{
models.CollectionChanged += models_CollectionChanged;
}
}
private void models_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
CollectionChanged -= viewModels_CollectionChanged;
try
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
{
foreach (var newItem in e.NewItems)
{
Add(viewModelConstructor((TModel)newItem));
}
break;
}
case NotifyCollectionChangedAction.Remove:
{
var viewModels = this.Where(viewModel => e.OldItems.Contains(viewModel.Model)).ToList();
foreach (var viewModel in viewModels)
{
Remove(viewModel);
}
break;
}
// TODO: Add missing actions
default: throw new NotImplementedException();
}
}
finally
{
CollectionChanged += viewModels_CollectionChanged;
}
}
/// <summary>
/// Only called once, by constructor
/// </summary>
private void CreateViewModelCollection()
{
foreach (var model in models)
{
Add(viewModelConstructor(model));
}
}
public void Dispose()
{
models.CollectionChanged -= models_CollectionChanged;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With