m0_37995419 2017-03-27 04:18 采纳率: 0%
浏览 3278

WPF MVVM TextBox绑定后台字符串

本人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;
        }
    }
  • 写回答

1条回答 默认 最新

  • IgoAheadNow 2017-03-28 03:26
    关注

    MainViewModel 是否有继承INotifyProperyChanged接口?

    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作