我嘗試做一些簡單的事情,但似乎我錯過了一些東西。當我點擊一個按鈕時,我嘗試更改標籤的內容。我使用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