-1
我有三個文本框用於存儲數值和三個靜態類,我想綁定到文本框。我希望第三個文本框等於前兩個文本框的總和。我有一個具備所有這些屬性(只顯示爲簡單起見值)的非靜態類:WPF綁定文本框屬性的靜態類不更新
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正在更新,但不是文本框文本。我不確定有什麼問題?