I have three textboxes meant to store numeric values, and three static classes with properties I would like to bind to the textboxes. I want the third textbox to equal the sum of the first two textboxes. I have a non-static class which has all of these properties (only showing Value for simplicity):
public class Field : PropertyValueChanged
{
private string _value;
public virtual string Value
{
get { return _value; }
set
{
if (value != _value)
{
_value = value;
NotifyPropertyChanged();
}
}
}
}
Then I have the three static classes:
public static class FirstValueField
{
public static Field FieldProperties;
static FirstValueField()
{
FieldProperties = new Field();
}
}
public static class SecondValueField
{
public static Field FieldProperties;
static SecondValueField()
{
FieldProperties = new Field();
}
}
public static class TotalField
{
public static Field FieldProperties = new Field();
static TotalField()
{
FirstValueField.FieldProperties.PropertyChanged += TotalField_PropertyChanged;
SecondValueField.FieldProperties.PropertyChanged += TotalField_PropertyChanged;
}
private static void TotalField_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
double val1, val2;
if (FirstValueField.FieldProperties.Value == null)
{
val1 = 0;
}
else
{
val1 = Double.Parse(FirstValueField.FieldProperties.Value);
}
if (SecondValueField.FieldProperties.Value == null)
{
val2 = 0;
}
else
{
val2 = Double.Parse(SecondValueField.FieldProperties.Value);
}
FieldProperties.Value = (val1 + val2).ToString();
MessageBox.Show("Changing TotalField to: " + FieldProperties.Value);
}
}
My xaml looks like this:
<TextBox x:Name="FirstValueField"
Text="{Binding Source={x:Static local:FirstValueField.FieldProperties},
Path=Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox x:Name="SecondValueField"
Text="{Binding Source={x:Static local:SecondValueField.FieldProperties},
Path=Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox x:Name="TotalField"
Text="{Binding Source={x:Static local:TotalField.FieldProperties},
Path=Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
The problem is the TotalField.FieldProperties.Value is updating, but not the textbox text. I'm not sure what's wrong?