模型更改更新View-Model WPF

God*_*ene 1 c# wpf mvvm

我有一个问题,我的模型更改更新回到我的viewmodel所以我可以显示.在这个例子中我有一个标签和一个按钮,当我按下按钮它将执行一些业务逻辑,并应更新屏幕上的标签.但是,当我的模型更改时,视图不会.我在这里做错了什么想法?

视图-

<Window.DataContext>
    <vm:ViewModel>
</Window.DataContext>
<Grid>
    <Label Content="{Binding Path=Name}"/>
    <Button Command={Binding UpdateBtnPressed}/>
</Grid>
Run Code Online (Sandbox Code Playgroud)

视图模型

public ViewModel()
{
    _Model = new Model();
}

public string Name
{
    get{return _Model.Name;}
    set
    {
        _Model.Name = value;
        OnPropertyChanged("Name");
    }
}

public ICommand UpdateBtnPressed
{
get{
_UpdateBtn = new RelayCommand(param => UpdateLabelValue());
return _UpdateBtn;
}

private void UpdateLabelValue()
{
    _Model.Name = "Value Updated";
}
Run Code Online (Sandbox Code Playgroud)

模型

private string name = "unmodified string";

public string Name
{
    get{return name;}
    set{name = value;}
}
Run Code Online (Sandbox Code Playgroud)

Mem*_*zer 6

试试这个:

private void UpdateLabelValue()
{
  Name = "Value Updated";
}
Run Code Online (Sandbox Code Playgroud)

  • 好点.通过直接更新模型,他们绕过了属性更改的通知. (3认同)