Пример #1
0
        public CategoryStatisticListViewModel(IDatabaseProvider <Expense> expenseProvider,
                                              IViewModelFactory viewModelFactory,
                                              ISelectedExpenseManager selectedManager
                                              )
        {
            this.expenseProvider  = expenseProvider;
            this.viewModelFactory = viewModelFactory;
            this.selectedManager  = selectedManager;

            LoadFromExpenseProvider();

            expenseProvider.DatabaseChanged += (sender, e) =>
            {
                if (e.ChangedAction == ChangedAction.Add)
                {
                    var expenseViewModel = viewModelFactory.CreateCategoryStatisticViewModel(e.DataItem);
                    expenseCategoryStatistics.Add(expenseViewModel);
                }
                else if (e.ChangedAction == ChangedAction.Remove)
                {
                    foreach (ViewModel expenseViewModel in expenseCategoryStatistics)
                    {
                        if (((CatagoryStatisticViewModel)expenseViewModel).Name.Equals(e.DataItem.Name))
                        {
                            expenseCategoryStatistics.Remove(expenseViewModel);
                            break;
                        }
                    }
                }
            };

            PropertyChanged += (sender, e) =>
            {
                if (e.PropertyName.Equals(nameof(SelectedCategory)))
                {
                    selectedManager.SelectedExpense = selectedCategory.Expense;
                }
            };
        }