private void AddStorageGroupIntoView(StorageGroupModel storageGroup, List <StorageModel> storages = null) { // create panel for storages _storageGroupPanel.Add(storageGroup.Id, new WrapPanel()); if (storages != null) { foreach (var currency in _currencies.OrderByDescending(_ => _.IsMain)) { // add storages into panel foreach (var storage in storages.Where(_ => _.StorageGroupId == storageGroup.Id && _.CurrencyId == currency.Id)) { AddStorageIntoView(storage); } } } var storageGroupViewModel = new StorageGroupViewModel(storageGroup) { AddStorageCommand = _viewModel.AddStorageCommand, DeleteCommand = _viewModel.DeleteStorageGroupCommand }; // create control for storage group data var storageGroupControl = new ContentControl() { Template = this.Resources["StorageGroupControlTemplate"] as ControlTemplate, Content = _storageGroupPanel[storageGroup.Id], DataContext = storageGroupViewModel }; _storageGroups.Add(storageGroupViewModel); StoragesPanel.Children.Add(storageGroupControl); }
public StorageGroupViewModel(StorageGroupModel model) : this() { Id = model.Id; Name = model.Name; UserId = model.UserId; PropertyChanged += (sender, e) => { if (e.PropertyName == nameof(Name)) { IsChanged = true; } }; IsChanged = false; }
private void InitializeViewModel() { _viewModel = new StoragesPageViewModel() { AddStorageCommand = new ParametrizedCommand <StorageGroupViewModel>( (item) => OpenDetails(new StorageViewModel() { UserId = GlobalVariables.UserId, StorageGroupId = item.Id, CurrencyId = _currencies.FirstOrDefault(_ => _.IsMain).Id }, true)), EditStorageCommand = new ParametrizedCommand <StorageViewModel>( (item) => OpenDetails(item)), DeleteStorageCommand = new ParametrizedCommand <StorageViewModel>( (item) => EntityViewHelper.ConfirmAndRemoveNamed(_service, item, () => { // remove in view _storageGroupPanel[item.StorageGroupId].Children.Remove(_storageView[item.Id]); NotifyDataChanged(); })), TransferMoneyCommand = new ParametrizedCommand <StorageViewModel>( (item) => OpenDetails(new MoneyTransferModel() { StorageFromId = item.Id, StorageFromCurrency = item.Currency }, true)), AddStorageGroupCommand = new Command( () => { // create storage group var storageGroup = new StorageGroupModel() { UserId = GlobalVariables.UserId }; // add storage group into database _storageGroupService.Add(storageGroup); // add storage group into view AddStorageGroupIntoView(storageGroup); // scroll down StoragesScrollViewer.ScrollToEnd(); NotifyDataChanged(); }), DeleteStorageGroupCommand = new ParametrizedCommand <StorageGroupViewModel>( (item) => EntityViewHelper.ConfirmAndRemoveNamed(_storageGroupService, item, () => { // update list _storageGroups.Remove(_storageGroups.First(_ => _.Id == item.Id)); // remove in view StoragesPanel.Children.Remove(_storageGroupPanel[item.Id].Parent as ContentControl); NotifyDataChanged(); })), AddMoneyTransferCommand = new Command( () => OpenDetails(new MoneyTransferModel(), true)), EditMoneyTransferCommand = new DataGridSelectedItemCommand <MoneyTransferModel>(GridMoneyTransfers, (item) => OpenDetails(item), null, true), DeleteMoneyTransferCommand = new DataGridSelectedItemsCommand <MoneyTransferModel>(GridMoneyTransfers, (items) => EntityViewHelper.ConfirmAndRemove(_moneyTransferService, items, () => { // remove in grid foreach (var item in items.ToList()) { _viewModel.MoneyTransfers.Remove(item); // update existing storages UpdateStorages(item); } NotifyDataChanged(); })) }; this.DataContext = _viewModel; }