Exemple #1
0
        private void LocalEntitiesOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
            case NotifyCollectionChangedAction.Add:
            {
                foreach (var newItem in e.NewItems)
                {
                    var viewModel = _provider.GetEntityViewModel((TEntity)newItem);
                    EntityViewModels.Add(viewModel);
                }
            }
            break;

            case NotifyCollectionChangedAction.Remove:
            {
                var viewModelsToDelete = new List <EntityViewModelBase <TEntity> >();

                foreach (var oldItem in e.OldItems)
                {
                    viewModelsToDelete.Add(EntityViewModels.First(x => x.Entity.Equals(oldItem)));
                }

                foreach (var entityViewModel in viewModelsToDelete)
                {
                    EntityViewModels.Remove(entityViewModel);
                }
            }
            break;
            }
        }
Exemple #2
0
        public EntityListViewModel(
            IUnityContainer container,
            ViewModelsProvider provider)
            : base(container)
        {
            container.RegisterInstance(this);
            _provider        = provider;
            PropertyNames    = provider.GetEntityViewModel <TEntity>().PropertyNames;
            Filter           = PropertyNames.FirstOrDefault();
            EntityViewModels = new ObservableRangeCollection <EntityViewModelBase <TEntity> >();
            var crud = container.Resolve <ICrud <TEntity> >();

            if (crud.LocalEntities != null)
            {
                crud.LocalEntities.CollectionChanged += LocalEntitiesOnCollectionChanged;

                foreach (var entity in crud.LocalEntities)
                {
                    var viewModel = _provider.GetEntityViewModel(entity);
                    EntityViewModels.Add(viewModel);
                }
            }

            CollectionView        = (ListCollectionView)CollectionViewSource.GetDefaultView(EntityViewModels);
            CollectionView.Filter = EntityFilter;
        }