我在将我的模型更改更新回我的 View 模型以便我可以显示时遇到问题。在这个例子中,我有一个标签和一个按钮,当我按下按钮时,它将执行一些业务逻辑,并且应该更新屏幕上的标签。但是,当我的模型更改时, View 不会。知道我在这里做错了什么吗?
查看-
<Window.DataContext>
<vm:ViewModel>
</Window.DataContext>
<Grid>
<Label Content="{Binding Path=Name}"/>
<Button Command={Binding UpdateBtnPressed}/>
</Grid>
View 模型
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";
}
型号
private string name = "unmodified string";
public string Name
{
get{return name;}
set{name = value;}
}
最佳答案
试试这个:
private void UpdateLabelValue()
{
Name = "Value Updated";
}
关于c# - 模型更改更新 View-Model WPF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14583770/