Cela serait assez facile si vous utilisiez la liaison (et un petit modèle MVVM). Réglez le DataContext de votre formulaire à quelque chose comme ceci:
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
}
}
Évidemment, il y a beaucoup de façons différentes de faire, mais si vous liez vos champs de saisie des valeurs ici, quand 1 zone de texte se change, il dira la view (via l'événement PropertyChanged) cette valeur et SubTotal ont été mis à jour. Ensuite, l'écran mettra à jour la valeur affichée de SubTotal en fonction de ce qui a été calculé.
Je peux développer ce point si vous n'êtes pas familier avec la liaison ou comment définir le DataContext de votre formulaire.