【发布时间】:2017-05-27 16:50:41
【问题描述】:
我有三个用于存储数值的文本框,以及三个带有我想绑定到文本框的属性的静态类。我希望第三个文本框等于前两个文本框的总和。我有一个具有所有这些属性的非静态类(为简单起见仅显示值):
public class Field : PropertyValueChanged
{
private string _value;
public virtual string Value
{
get { return _value; }
set
{
if (value != _value)
{
_value = value;
NotifyPropertyChanged();
}
}
}
}
那么我就有了三个静态类:
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);
}
}
我的 xaml 看起来像这样:
<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}"/>
问题是 TotalField.FieldProperties.Value 正在更新,但不是文本框文本。不知道怎么回事?
【问题讨论】: