public class MainStudentModel:ViewModelBase
{
//实体
private StudentModel stu = new StudentModel();
public string StuName
{
get { return stu.Name; }
set
{
stu.Name = value;
// 激活属性
RaisePropertyChanged("StuName");
}
}
public string StuAddress
{
get { return stu.Address; }
set
{
stu.Address = value;
RaisePropertyChanged("StuAddress");
}
}
public RelayCommand ChangeStuCommand
{
get;
private set;
}
public MainStudentModel()
{
if (!IsInDesignMode)
{
stu = new StudentModel();
StuName = "xiaoming";
StuAddress = "朝阳区北苑二号院";
ChangeStuCommand=new RelayCommand(() =>
{
StuName = "taiyang";
StuAddress = "朝阳区duanwumen";
});
}
}
}
<TextBlock HorizontalAlignment="Left" Margin="80,66,0,0" TextWrapping="Wrap" Text="姓名" VerticalAlignment="Top"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="143,65,0,0" TextWrapping="Wrap" Text="{Binding StuName}" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.517,0.043"/>
<TextBlock HorizontalAlignment="Left" Margin="80,128,0,0" TextWrapping="Wrap" Text="地址" VerticalAlignment="Top"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="143,120,0,0" TextWrapping="Wrap" Text="{Binding StuAddress}" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.517,0.043"/>
<Button Content="显示" HorizontalAlignment="Left" Margin="143,240,0,0" VerticalAlignment="Top" Width="75" Command="{Binding ChangeStuCommand}"/>
public StudentView()
{
InitializeComponent();
//MainStudentModel
this.DataContext = new MainStudentModel();
}