如何使用mvvm模式更改标签内容 [英] how to change label content using mvvm pattern
问题描述
我尝试做一些简单的事情,但是好像我错过了一些事情.单击按钮时,我尝试更改标签的内容.我使用MVVM模式进行操作.这是我的代码:
I try to do something easy but seems like i miss something. I try to change the content of a label when I click on a button. I do that using MVVM pattern. here is my code :
查看:
<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"
/>
ViewModel:
ViewModel :
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:更改的功能有效,因为我放了一个断点看.谢谢您的帮助
at first the label is "etape1" as in the constructor, but then when i click on the next button the value doesn't change. ps : the fonction to change works because i put a breakpoint to see. Thanks for your help
推荐答案
您必须使用属性,而不是后备字段.
You have to use the property, not the backing field.
private void GoNext(object obj)
{
switch (_path)
{
case "etape1":
path = "etape2"; // without underscore
break;
case : "etape2"
path = "etape3";
break;
case "etape3":
path = "etape4";
break;
default:
path = " ";
break;
}
这将引发有关更改的通知
That will raise the notification for the change
修改
最好为ViewModel使用ViewModelBase
It's also better using a ViewModelBase
for your ViewModel
public abstract class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
您可以在其中定义
protected virtual void RaisePropertyChanged(string propertyName)
{
this.VerifyPropertyName(propertyName);
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
var e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}
替代
根据此注释,我们可以介绍 CallerMemberName
As per this comment, we can introduce the CallerMemberName
protected virtual void RaisePropertyChanged<T>([CallerMemberName] string propertyName = null)
另一个有用的变体是基于选择器的,当您需要从不同设置器
Another useful variation is based on a selector, when you need to raise it for a dependent property from a different setter
protected virtual void RaisePropertyChanged<T>(Expression<Func<T>> selectorExpression)
{
if (selectorExpression == null)
throw new ArgumentNullException("selectorExpression");
MemberExpression body = selectorExpression.Body as MemberExpression;
if (body == null)
throw new ArgumentException("The body must be a member expression");
RaisePropertyChanged(body.Member.Name);
}
这篇关于如何使用mvvm模式更改标签内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!