我正在玩数据绑定,我创建了一个这样的用户控件(视图):
<UserControl>
<Grid x:Name="LayoutRoot"
Background="{StaticResource PhoneChromeBrush}"
DataContext="{Binding Source={StaticResource ViewModelSampleDataSource}}"
>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<TextBox Text="{Binding Model.Var1, Mode=TwoWay}" InputScope="Number" Grid.Row="0" FontSize="90"/>
<TextBlock Text="{Binding Model.Var2}" Grid.Row="1" FontSize="90" />
</Grid>
我有这个类模型
public class ModelSample:INotifyPropertyChanged
{
public ModelSample()
{
}
private double var1;
public double Var1
{
get { return var1; }
set
{
var1 = value;
OnPropertyChanged("var");
}
}
private double var2;
public double Var2
{
get { return var2; }
set
{
var2 = value;
OnPropertyChanged("var2");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(name));
}
}
这是 ViewModel
public class ViewModelSample
{
private static ModelSample model=new ModelSample();
public static ModelSample Model
{
get { return model; }
set { model = value; }
}
}
我的问题是:当我更改 var1 的值(使用文本框)时,我想更新 var2 的值,我该怎么做?谢谢