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); }); }
public DebtPenaltyViewModel(DebtPenaltyModel penalty) : this() { Id = penalty.Id; Date = penalty.Date; Description = penalty.Description; Value = penalty.Value; DebtId = penalty.DebtId; this.PropertyChanged += (sender, e) => { // not use reflection if (e.PropertyName == nameof(Date)) { penalty.Date = Date; } else if (e.PropertyName == nameof(Description)) { penalty.Description = Description; } else if (e.PropertyName == nameof(Value)) { penalty.Value = Value; } }; }
private void AddPenaltyToView(DebtPenaltyModel penalty) { // TODO: should be updated the same penalty object var penaltyViewModel = new DebtPenaltyViewModel(penalty) { DeleteCommand = DeletePenaltyCommand }; var penaltyControl = new ContentControl() { Template = this.Resources["PenaltyItemControlTemplate"] as ControlTemplate, DataContext = penaltyViewModel }; _penaltyControl.Add(penaltyViewModel, penaltyControl); PenaltiesPanel.Children.Add(penaltyControl); }