在 Silverlight 中使用 MVVM,在我看来,我有:
<TextBox Text="{Binding Path=Temperature,Mode=TwoWay}" />
MyViewModel 有:
public MyModel Model {get{...}set{...}}//In my code I do have the property changed events for this
public string Temperature
{
get
{
return Model.Temperature.ToString();
}
set
{
double test;
if(double.TryParse(value, out test))
{
Model.Temperature = test;
}
else
{
Model.Temperature = 0D;
}
}
}
void Model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
switch(e.PropertyName)
{
case "Temperature":
OnPropertyChanged("Temperature");
break;
}
}
MyModel 有:
private double _temperature;
public double Temperature
{
get
{
return _temperature;
}
set
{
_temperature = value; OnPropertyChanged("Temperature");
}
}
如果用户输入的内容不会解析为双精度,则 Model.Temperature 属性设置为 0,我希望 TextBox 也更改为 0。我认为 OnPropertyChanged 事件会解决这个问题。任何想法发生了什么?