我尝试做一些简单的事情,但似乎我错过了一些东西。当我点击一个按钮时,我尝试更改标签的内容。我使用MVVM模式来做到这一点。这里是我的代码:如何使用mvvm模式更改标签内容
查看:
<Button x:Name="buttonNext"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Grid.Column="1"
Grid.Row="2"
Width="85"
Height="35"
Style="{StaticResource AccentedSquareButtonStyle}"
Command="{Binding Path=Next}">
<TextBlock Text="Next"
TextWrapping="Wrap"
TextAlignment="Center"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Button>
<Label Name="Path"
Grid.ColumnSpan="2"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Content="{Binding path}"
FontWeight="Bold"
Foreground="DeepSkyBlue"
/>
视图模型:
public ICommand Next { get; set; }
private string _path;
public string path
{
get
{
return _path;
}
set
{
_path = value;
RaisePropertyChanged("path");
}
}
public event PropertyChangedEventHandler PropertyChanged = null;
protected virtual void RaisePropertyChanged(string propName)
{
if (PropertyChanged != null)
{
Task.Run(() => PropertyChanged(this, new PropertyChangedEventArgs(propName)));
}
}
public page4ViewModel(NavigationViewModel navigationViewModel)
{
_path = "etape1";
Next = new BaseCommand(GoNext);
}
private void GoNext(object obj)
{
switch (_path)
{
case "etape1":
_path = "etape2";
break;
case : "etape2"
_path = "etape3";
break;
case "etape3":
_path = "etape4";
break;
default:
_path = " ";
break;
}
}
最初的标签是“etape1”在构造函数中,但后来当我点击下一步按钮值不会改变。 ps:改变作品的功能,因为我把一个断点看。感谢您的帮助
你为什么在任务中提升'propertychanged'异步? – Mat