IObservableCollection<ViewModel<T>> wrapping Obs

本文关键字:lt gt wrapping Obs ViewModel IObservableCollection | 更新日期: 2023-09-27 18:14:52

在c#/WPF中,提供一个实现IObservableCollection<ViewModel<T>>并从模型中包装ObservableCollection<T>的类的最佳方法是什么?

模型可以向底层集合添加/移除T个元素,或者视图可以从视图模型层添加/移除ViewModel元素。

从MVVM模式的角度来看,这有什么可疑的吗?是否有包含此类或类似类的库?

IObservableCollection<ViewModel<T>> wrapping Obs

我在下面写了一个类,您可以将其用于单线程目的。我只实现了添加和删除,所以你必须实现你可能使用的任何其他操作:…

用法:

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;
    }
}