tags:

views:

219

answers:

1

Hi,

I have a wpf xaml form which has 5 text boxes shows order price. Below the 5 text boxes i have another textbox:[subTotal] which displays the subtotal of order price's."SubTotal" Textbox should display the subtotal of order prices automatically.

Is there any XAMl coding way where i can calculate and dispaly total in the "SubTotal" text box automatically, when user enters a value in the order prices text boxes.

Thanks & Regards

SNA

+1  A: 

This would be quite easy if you used binding (and a little MVVM pattern). Set the DataContext of your form to something like this:

using System;
using System.ComponentModel;

namespace WpfApplication1
{
    [Serializable]
    public class TestViewModel : INotifyPropertyChanged
    {
     private decimal value1;
     private decimal value2;
     private decimal value3;
     private decimal value4;
     private decimal value5;

     public TestViewModel()
     {
     }

     public decimal Value1
     {
      get { return value1; }
      set
      {
       value1 = value;
       RaisePropertyChangedEvent("Value1");
       RaisePropertyChangedEvent("SubTotal");
      }
     }
     public decimal Value2
     {
      get { return value2; }
      set
      {
       value2 = value;
       RaisePropertyChangedEvent("Value2");
       RaisePropertyChangedEvent("SubTotal");
      }
     }
     public decimal Value3
     {
      get { return value3; }
      set
      {
       value3 = value;
       RaisePropertyChangedEvent("Value3");
       RaisePropertyChangedEvent("SubTotal");
      }
     }
     public decimal Value4
     {
      get { return value4; }
      set
      {
       value4 = value;
       RaisePropertyChangedEvent("Value4");
       RaisePropertyChangedEvent("SubTotal");
      }
     }
     public decimal Value5
     {
      get { return value5; }
      set
      {
       value5 = value;
       RaisePropertyChangedEvent("Value5");
       RaisePropertyChangedEvent("SubTotal");
      }
     }
     public decimal SubTotal
     {
      get
      {
       return this.value1 + this.value2 + this.value3 + this.value4 + this.value5;
      }
     }

     #region INotifyPropertyChanged Members

     public event PropertyChangedEventHandler PropertyChanged;
     private void RaisePropertyChangedEvent(string propertyName)
     {
      if (this.PropertyChanged != null)
       this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
     }

     #endregion
    }
}

Obviously there are a lot of different ways to do this, but if you bind your textboxes to the values here, when 1 textbox gets changed it will tell the view (via the PropertyChanged event) that value and SubTotal were updated. Then the screen will update the displayed value of SubTotal based on what was computed.

I can expand on this if you're not familiar with binding or how to set the DataContext of your form.

opedog