[SOLVED] Dynamically Change value of a text box when changing datagrid cells in C# WPF

Issue

This Content is from Stack Overflow. Question asked by Tharindu

I Need a help. I created a datagrid and the value column of that grid is automatically calculated When Entering the unit price and hours using INotifyPropertyChanged.(Please see the attached picture). But I don’t know how to get the Total value of that value column (sum of all values) automatically to total amount text box dynamically (Without using any button to calculate).( and also, if I delete a row then the value of that row should be automatically deducted from the Total amount.

enter image description here

Property Code :

public double UnitPrice
    {
        get { return _UnitPrice; }
        set { _UnitPrice = value; OnPropertyChanged("UnitPrice"); OnPropertyChanged("Value"); }
    }

    public double Hours
    {
        get { return _Hours; }
        set { _Hours = value; OnPropertyChanged("Hours"); OnPropertyChanged("Value"); }
    }

    public double Value
    {
        get { return _Value = UnitPrice * Hours; }
        set { _Value = value; OnPropertyChanged("Value"); }
    }

Your help is highly appreciated.

Thank you!



Solution

First Wrap your parent class(that holds all these properties) inside an ObservableCollection<MyT> Ledger, bind to this inside your View.

Next create an event handler for CollectionChanged

Ledger.CollectionChanged += Ledger_CollectionChanged;

your collection changed handler:

private void Ledger_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        if (e.NewItems != null)
            foreach (LedgerTransaction transaction in e.NewItems)
                transaction.PropertyChanged += Transaction_PropertyChanged;

        if (e.OldItems != null)
            foreach (LedgerTransaction transaction in e.OldItems)
                transaction.PropertyChanged -= Transaction_PropertyChanged;

        OnPropertyChanged(nameof(CanSave));
        OnPropertyChanged(nameof(TotalCredit));
        OnPropertyChanged(nameof(TotalDebit));
        OnPropertyChanged(nameof(Difference));
    }

and finally your property changed handler

private void Transaction_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        switch (e.PropertyName)
        {
            case "Debit":
                OnPropertyChanged(nameof(TotalDebit));
                break;
            case "Credit":
                OnPropertyChanged(nameof(TotalCredit));
                break;
            default:
                break;
        }

        OnPropertyChanged(nameof(CanSave));
    }

Create Total properties and bind to it where you want to show your total, in my case:

    public decimal TotalDebit { get => Ledger.Sum(x => x.Debit); }
    public decimal TotalCredit { get => Ledger.Sum(x => x.Credit); }


This Question was asked in StackOverflow by Tharindu and Answered by AMunim It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?