0

I am trying to make an invoice program, and i am trying to show the objects in a dynamic way inside a DataGrid. There would be only one column that the object has "Name", and extra columns like tax%, discount % and amount.(As an IntegerUpDown from xceed Toolkit).

Now what i need is another column that will show the total price by taking the price per product(part of the original object), multiplying it by the amount, adding tax and removing discount%.

I have seen many implementations, but i cant understand the logic behind it. I will create an object (Item) with the attributes that i need (name, price) and some functions like "total_Price" and event PropertyChangedEventHandler? But how i will be able to connect the "tax" , "discount" that are at the same row? If i add them into the object Item and hence to be able to refer to them, how i will be able to manipulate them through the DataGrid. If i change them there (at the DataGrid through the IntegerUpDown button), will they change in the actual object?

I am really new with wpf and especially with data-templates and stuff so i cant really get my head around the logic of it! A hint would be good at least on how i could start!

mm8
  • 163,881
  • 10
  • 57
  • 88
Feronimus
  • 123
  • 10
  • If your ViewModel is binded to the datagrid and correcty implemented, sure, its properties will change when you manipulate the grid. – Babbillumpa Jul 12 '18 at 09:30

1 Answers1

1

You could add a read-only property to your data class that returns the calculated total price. Don't forget to implement the INotifyPropertyChanged interface raise the PropertyChanged event for this property whenever any of the other properties are changed:

public class Invoice
{
    public string Name { get; set; }

    private double _price;
    public double Price
    {
        get { return _price; }
        set { _price = value; NotifyPropertyChanged(nameof(TotalPrice)); }
    }

    private double _tax;
    public double Tax
    {
        get { return _tax; }
        set { _tax = value; NotifyPropertyChanged(nameof(TotalPrice)); }
    }

    private double _discount;
    public double Discount
    {
        get { return _discount; }
        set { _discount = value; NotifyPropertyChanged(nameof(TotalPrice)); }
    }

    private double _amount;
    public double Amount
    {
        get { return _amount; }
        set { _amount = value; NotifyPropertyChanged(nameof(TotalPrice)); }
    }

    public double TotalPrice
    {
        get
        {
            return (_price * _amount + _tax) * (1 - _discount);
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Thats really good! Thank you! So now i have to databind the List with my Datagrid. Till now i am only using lists and ItemsSource. I believe this might work. But just to be sure, is that the right way to bind a IEnumerable entity to a WPF Datagrid? – Feronimus Jul 12 '18 at 09:42
  • 1
    Yes, bind the `ItemsSource` property to your `List`. – mm8 Jul 12 '18 at 09:43