Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

EventAggregator help

$
0
0

I have a small project with 

an Account Module : AccountListViewModel(display the entire list of accounts) , AccountViewModel(add/edit),

TransactionListView(a list of transactions belonging to an account) and TransactionViewModel(add/edit)

a Categories Module : CategoryListViewModel(display the entire list), CategoryViewModel(add/edit)

a ReportModule : ReportViewModel(collections that serve as itemssource for listviews/charts)

The direction I was thinking of is using EventAggregator to comunicate between viewmodels.

An Account can contain a list of Transactions. Then each Transaction has a list of categories and selected category property.

A Category has a name, income and expense property.

A  transaction has  a combobox to display a list of categories and one to select which type(deposit/withdrawal) and some others like ammount, date.

The behavior I want to achieve is:

If I add a category and then go to add or edit a transaction then the combobox will have the newly added category same applies if I remove one.

If I edit the details of a category then same when I go to add/edit a transaction then the category I edited should be updated in the combobox. If the category I edited was already selected for a transaction then it should change as well.

TransactionViewModel class :

First constructor is used to initialize/edit existing transactions and second one is called when I create a new transaction. 

                 

internal TransactionViewModel(TransactionModel transaction, IEventAggregator eventAggregator)
        {
             this.eventAggregator = eventAggregator;
             TransactionId = transaction.TheTransaction.TransactionId;
             ammount = (double)transaction.TheTransaction.TransactionAmmount;
             PayeeId = transaction.TheTransaction.PayeeId;
             CategoryId = transaction.TheTransaction.TransactionCategoryId;
             Categories = GetCategories();
             SelectedCategory = categories.Where(i => i.CategoryId == CategoryId).FirstOrDefault();

        }

        internal TransactionViewModel(IEventAggregator eventAggregator)
        {
            this.eventAggregator = eventAggregator;
            Categories= GetCategories(); 
        }

    The Categories collection :

            private ObservableCollection<CategoryViewModel> categories;
            private CategoryViewModel selectedCategory;

 public CategoryViewModel SelectedCategory
            {
                get { return selectedCategory; }
                set
                {
                    selectedCategory = value;
                    OnPropertyChanged("SelectedCategory");
                }
            }

public ObservableCollection<CategoryViewModel> Categories
            {
                get { return categories; }
                set           
                {
                    categories = value;
                    this.eventAggregator.GetEvent<TransactionCategoryEvent>().Subscribe(item => this.Categories.Add(new CategoryViewModel(item, eventAggregator)));
                    this.eventAggregator.GetEvent<TransactionCategoryRemovedEvent>().Subscribe(item =>
                        {
                            var it = Categories.Where(i => i.CategoryId == item.TheCategory.CategoryId).FirstOrDefault();
                            Categories.Remove(it);
                        });
                    this.eventAggregator.GetEvent<TransactionCategoryUpdatedEvent>().Subscribe(item =>
                        {
                             var _selectedCategory = Categories.Where(i => i.CategoryId == item.TheCategory.CategoryId).FirstOrDefault();
                            Categories.Clear();
                           // GetCategories();
                            foreach(var myItem in GetCategories())
                            {
                                Categories.Add(myItem);
                                if (item.TheCategory.CategoryId == _selectedCategory.CategoryId)
                                {
                                    SelectedCategory.CategoryName = item.TheCategory.CategoryName;
                                    SelectedCategory.CategoryIncome = (double)item.TheCategory.CategoryIncome;
                                    SelectedCategory.CategoryExpense = (double)item.TheCategory.CategoryExpense;
                                }
                            }
                          
                        });
                    OnPropertyChanged("Categories");
                }
            }

            private ObservableCollection<CategoryViewModel> GetCategories()
            {
                if (categories == null)
                    categories = new ImprovedObservableCollection<CategoryViewModel>();
                categories.Clear();

                using (var context = new Ents())
                {
                    foreach (var category in context.TransactionCategories)
                    {
                        TransactionCategoryModel tcm = new TransactionCategoryModel() { TheCategory = category};
                        categories.Add(new CategoryViewModel(tcm, eventAggregator));
                    }
                }
                return categories;

In the setter of the Categories list I subscribe to adding/removing and updating.

The TransactionCategoryEvent and TransactionCategoryRemovedEvent are working. But the TransactionCategoryUpdatedEvent sometimes is fired and it updates the combobox and transactionlist fine other times it doesnt.

Any sugestions ?



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>