在C#/ WPF中,提供一个实现IObservableCollection<ViewModel<T>>
并从模型中回绕ObservableCollection<T>
的类的最佳方法是什么?
模型可以向底层集合添加/删除T元素,或者视图可以从视图模型层添加/删除ViewModel元素。
从MVVM模式的角度来看,有什么可疑的吗?是否有包含此类或类似类的库?
答案 0 :(得分:1)
我在下面编写了一个可用于单线程目的的类。我只实现了添加和删除,因此您必须实施您可能使用的任何其他操作:...
用法:
var viewModelWrapper = new ObservableCollectionViewModel<ItemViewModel, Model>(Model.List, item => new ItemViewModel(item));
泛型类 - 可观察集合视图模型包装器:
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;
}
}