public static bool ConfirmAndRemove <T>(IEnumerable <T> entities, Action <IEnumerable <T> > deleteAction, string entityName, IEnumerable <string> descriptions, Action success = null) where T : class { var message = MultiLangResource.DeletionConfirmationMessage(typeof(T), descriptions); if (MessageBox.Show(message, MultiLangResourceManager.Instance[MultiLangResourceName.DeletionConfirmation], MessageBoxButton.YesNo, MessageBoxImage.Exclamation, MessageBoxResult.Yes) == MessageBoxResult.Yes) { try { // remove in database deleteAction(entities); } catch (Exception ex) { MessageBox.Show(MultiLangResource.DeletionErrorMessage(typeof(T), descriptions), MultiLangResourceManager.Instance[MultiLangResourceName.DeletionError], MessageBoxButton.OK, MessageBoxImage.Error); return(false); } // apply if success result success?.Invoke(); return(true); } return(false); }
protected override void InitializeCommands() { base.InitializeCommands(); DeletePenaltyCommand = new ParametrizedCommand <DebtPenaltyViewModel>( (item) => { var message = MultiLangResource.DeletionConfirmationMessage(typeof(DebtPenaltyModel), new[] { item.Description }); if (MessageBox.Show(message, MultiLangResourceManager.Instance[MultiLangResourceName.DeletionConfirmation], MessageBoxButton.YesNo, MessageBoxImage.Exclamation, MessageBoxResult.Yes) == MessageBoxResult.Yes) { // remove from entity WrappedEntity.Entity.Penalties.Remove(WrappedEntity.Entity.Penalties.First(_ => _.Id == item.Id)); // remove from view PenaltiesPanel.Children.Remove(_penaltyControl[item]); } }); WrappedEntity.Entity.AddPenaltyCommand = new Command(() => { var newPenalty = new DebtPenaltyModel() { DebtId = WrappedEntity.Entity.Id, Id = WrappedEntity.Entity.Penalties.Count > 0 ?WrappedEntity.Entity.Penalties.Min(_ => _.Id) - 1 : -1 }; WrappedEntity.Entity.Penalties.Add(newPenalty); AddPenaltyToView(newPenalty); }); }
private void InitializeViewModel() { _viewModel = new CurrenciesPageViewModel() { // Currency commands AddCurrencyCommand = new Command( () => OpenDetails(new CurrencyModel() { UserId = GlobalVariables.UserId }, true)), EditCurrencyCommand = new DataGridSelectedItemCommand <CurrencyModel>(GridCurrencies, (item) => OpenDetails(item), null, true), DeleteCurrencyCommand = new DataGridSelectedItemsCommand <CurrencyModel>(GridCurrencies, (items) => EntityViewHelper.ConfirmAndRemoveNamed(_service, items, () => { // remove in grid foreach (var item in items.ToList()) { _viewModel.Currencies.Remove(item); } NotifyDataChanged(); }), (items) => !items.Any(_ => _.IsMain)), SetMainCommand = new DataGridSelectedItemCommand <CurrencyModel>(GridCurrencies, (item) => { // save main in database _service.SetMain(item); // refresh currency data in grid UpdateMainCurrencyLocal(item); // refresh commands RefreshCurrencyCommandsState(); NotifyDataChanged(); }, (item) => !item.IsMain), ChangeActivityCommand = new DataGridSelectedItemsCommand <CurrencyModel>(GridCurrencies, (items) => { // get new place index var firstNotUsed = _viewModel.Currencies.FirstOrDefault(_ => !_.IsActive); var newIndex = firstNotUsed != null ? _viewModel.Currencies.IndexOf(firstNotUsed) : _viewModel.Currencies.Count - 1; // update currencies foreach (var c in items) { c.IsActive = !c.IsActive; // replace in grid _viewModel.Currencies.Move(_viewModel.Currencies.IndexOf(c), newIndex); if (c.IsActive) { newIndex++; } } // update currencies in database _service.Update(items); RefreshCurrencyCommandsState(); NotifyDataChanged(); }, (items) => items.Select(e => e.IsActive).Distinct().Count() == 1), // Currency echange rate commands AddCurrencyExchangeRateCommand = new Command( () => OpenDetails(new CurrencyExchangeRateModel(), true)), EditCurrencyExchangeRateCommand = new DataGridSelectedItemCommand <CurrencyExchangeRateModel>(GridCurrencyExchangeRates, (item) => OpenDetails(item), doubleClick: true), DeleteCurrencyExchangeRateCommand = new DataGridSelectedItemsCommand <CurrencyExchangeRateModel>(GridCurrencyExchangeRates, (items) => { var message = MultiLangResource.DeletionConfirmationMessage(typeof(CurrencyExchangeRateModel), items.Select(_ => $"{_.CurrencyFrom.Name} -> {_.CurrencyTo.Name}")); if (MessageBox.Show(message, MultiLangResourceManager.Instance[MultiLangResourceName.DeletionConfirmation], MessageBoxButton.YesNo, MessageBoxImage.Exclamation, MessageBoxResult.Yes) == MessageBoxResult.Yes) { // remove in database _currencyExchangeRateService.Delete(items); // remove in grid foreach (var item in items.ToList()) { _viewModel.CurrencyExchangeRates.Remove(item); } NotifyDataChanged(); } }) }; this.DataContext = _viewModel; }