title: Display total sale from sum of datagrid column in C# WPF application
body: <p><a href="https://i.stack.imgur.com/6HPoH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6HPoH.png" alt="enter image description here"></a></p>
  
  <p>I am having trouble understanding how to bind the total sale textbox on the UI to the sum of the total price in the datagrid. I have read several articles, tried to implement a sum procedure and bind it, tried using collection changed notification and I can't get the total sale to work. I have an item class that has a Quantity and UnitPrice properties with a calculated TotalPrice which automatically updates when quantity or unit price changes. The list of items is contained in a SalesOrder class. The ui is the main window which I am using to create the order.
  My code is at: <a href="https://github.com/battondl/SalesOrder/tree/master/simpleShoppingListProgram" rel="nofollow noreferrer">https://github.com/battondl/SalesOrder/tree/master/simpleShoppingListProgram</a>
  Any guidance on how to get to get the total price on the UI textbox to update automatically would be much appreciated.</p>
  
  <p>Each line item in the datagrid has a total price based on quanity * unit price.</p>
  
  <p>It is the Total Sale price that I am trying to update which is a sum of all the line items totalprice.</p>
  
  <pre><code>class Item : INotifyPropertyChanged
  {
      private string partNumber;
      private int quantity;
      private decimal unitPrice;
      private DateTime orderDate;
  
  
      public string PartNumber
      {
          get { return partNumber; }
          set
          {
              partNumber = value;
              OnPropertyChanged();
          }
      }
  
      public int Quantity
      {
          get { return quantity; }
          set
          {
              quantity = value;
              OnPropertyChanged("Quantity");
              OnPropertyChanged("TotalPrice");
          }
      }
      public decimal UnitPrice
      {
          get { return unitPrice; }
          set
          {
              unitPrice = value;
              OnPropertyChanged("UnitPrice");
              OnPropertyChanged("TotalPrice");
          }
      }
  
      public DateTime OrderDate
      {
          get { return orderDate; }
          set
          {
              orderDate = value;
              OnPropertyChanged();
          }
      }
  
      public decimal TotalPrice =&gt; Quantity * UnitPrice;
  
      public Item()
      {
          PartNumber = "";
          Quantity = 0;
          UnitPrice = 0.00m;
          OrderDate = DateTime.Now;
      }
  
  
      public event PropertyChangedEventHandler PropertyChanged;
      private void OnPropertyChanged([CallerMemberName] string caller = "")
      {
          if (PropertyChanged != null)
          {
              PropertyChanged(this, new PropertyChangedEventArgs(caller));
          }
      }
  
  
  }
  
  
  
  class SalesOrder 
  {
  
      private string orderNumber;
      public string OrderNumber
      {
          get { return orderNumber; }
          set
          {
              orderNumber = value;
          }
      }
  
      private ObservableCollection&lt;Item&gt; items;
  
      public ObservableCollection&lt;Item&gt; Items
      {
          get { return items; }
      }
  
      public SalesOrder()
      {
          orderNumber = "";
          items = new ObservableCollection&lt;Item&gt;();
  
      }
  
      public decimal CalculateTotalPrice()
      {
          decimal total = 0.00m;
          foreach (Item item in items)
          {
              total += item.TotalPrice;
          }
          return total;
      }
  }
  
  public partial class MainWindow : Window
  {
      private SalesOrder salesOrder;
  
      public MainWindow()
      {
          InitializeComponent();
      }
  
      private void Window_Loaded(object sender, RoutedEventArgs e)
      {
          salesOrder = new SalesOrder();
          dgSaleItems.ItemsSource = salesOrder.Items;
  
          tbx_totalSale.Text = salesOrder.CalculateTotalPrice().ToString();
      }
  }
  
  
  &lt;Grid&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="*"/&gt;
          &lt;RowDefinition Height="6*"/&gt;
          &lt;RowDefinition Height="*"/&gt;
      &lt;/Grid.RowDefinitions&gt;
      &lt;StackPanel Orientation="Vertical"
          Grid.Row="0"&gt;
          &lt;StackPanel Orientation="Horizontal"&gt;
              &lt;Label Content="OrderNumber: " /&gt;
              &lt;TextBox Name="tbx_orderNumber" 
                       Width="100"/&gt;
          &lt;/StackPanel&gt;
      &lt;/StackPanel&gt;
      &lt;StackPanel Orientation="Vertical"
                  Grid.Row="1"&gt;
          &lt;DataGrid Name="dgSaleItems"
          Grid.Row="1"
                    AutoGenerateColumns="False"&gt;
              &lt;DataGrid.Columns&gt;
                  &lt;DataGridTextColumn x:Name="partNumber"
                                      Header="Part Number"
                                      Binding="{Binding Path=PartNumber}" /&gt;
                  &lt;DataGridTextColumn x:Name="quantity"
                                      Header="Quantity"
                                      Binding="{Binding Path=Quantity}" /&gt;
                  &lt;DataGridTextColumn x:Name="unitPrice"
                                      Header="Unit Price"
                                      Binding="{Binding Path=UnitPrice}" /&gt;
                  &lt;DataGridTextColumn x:Name="totalPrice"
                                      Header="Total Price"
                                      Binding="{Binding Path=TotalPrice}" /&gt;
                  &lt;DataGridTemplateColumn x:Name="orderDate"
                                          Header="Order Date"&gt;
                      &lt;DataGridTemplateColumn.CellTemplate&gt;
                              &lt;DataTemplate&gt;
                                  &lt;TextBlock Text="{Binding Path=OrderDate}"          /&gt;
                              &lt;/DataTemplate&gt;
                          &lt;/DataGridTemplateColumn.CellTemplate&gt;
                          &lt;DataGridTemplateColumn.CellEditingTemplate&gt;
                              &lt;DataTemplate&gt;
                              &lt;DatePicker SelectedDate="{Binding Path=OrderDate}" /&gt;
                              &lt;/DataTemplate&gt;
                          &lt;/DataGridTemplateColumn.CellEditingTemplate&gt;
                      &lt;/DataGridTemplateColumn&gt;
              &lt;/DataGrid.Columns&gt;
          &lt;/DataGrid&gt;
      &lt;/StackPanel&gt;
      &lt;StackPanel Orientation="Vertical"
                  Grid.Row="7"&gt;
          &lt;StackPanel Orientation="Horizontal"&gt;
              &lt;Label Content="Total Sale: " /&gt;
              &lt;TextBox x:Name="tbx_totalSale"
                       Width="100"/&gt;
          &lt;/StackPanel&gt;
      &lt;/StackPanel&gt;
  &lt;/Grid&gt;
  </code></pre>