本人WPF菜鸟,我想把TextBox控件数据绑定在Person类的Age字段上,但界面并不能更新,请大神帮忙,谢谢
界面绑定代码:
界面类部分代码:
MainViewModel mainViewModel = new MainViewModel();
public MainWindow()
{
InitializeComponent();
grid.DataContext = mainViewModel;
}
ViewModel部分代码:
public MainViewModel()
{
this.ShowInfoClick = new DelegateCommand(ShowInfoExec);
this.UpdateInfoClick = new DelegateCommand(UpdateExec);
this.Name = p1.Name;
this.Age = p1.Age;
this.Hobby = p1.Hobby;
}
public void UpdateExec()
{
p1.Age = p1.Age + 1;
this.Age = p1.Age;
this.Name = p1.Name;
}
private int _age = 24;
public int Age
{
set
{
_age = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Age"));//对Age进行监听
}
}
get
{
return _age;
}
}
Person类部分代码:
public class Person:INotifyPropertyChanged
{
private String _name = "张三";
private int _age = 24;
private String _hobby = "篮球";
public int Age
{
set
{
_age = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Age"));//对Age进行监听
}
}
get
{
return _age;
}
}