2017-11-18 215 views
0

我正在更改类构造函数中的标签,它工作正常,标签更新(“0”)。我也在尝试更新标签,当我点击一个按钮,但它不工作(“X”)。我注意到调试标签值已更新,PropertyChanged被触发,但视图不会更改。PropertyChanged被触发,但视图未更新

public class HomeViewModel : ViewModelBase 
{ 
    string playerA; 
    public string PlayerA 
    { 
     get 
     { 
      return playerA; 
     } 
     set 
     { 
      playerA = value; 
      this.Notify("playerA"); 
     } 
    } 

    public ICommand PlayerA_Plus_Command 
    { 
     get; 
     set; 
    } 

    public HomeViewModel() 
    { 
     this.PlayerA_Plus_Command = new Command(this.PlayerA_Plus); 
     this.PlayerA = "0"; 
    } 

    public void PlayerA_Plus() 
    { 
     this.PlayerA = "X"; 
    } 
} 



public abstract class ViewModelBase : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    protected void Notify(string propertyName) 
    { 
     if (this.PropertyChanged != null) 
      this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 
+1

在这里写下你的xaml。我的意思是按钮和标签+将[string platerA;]更改为[string playerA =“!”;]如果您的标签不显示!毕竟,你的绑定有一个问题。 –

回答

4

在您的PropertyChangedEventArgs中传递的参数的名称是错误的。您正在使用“playerA”,但(public)属性的名称是“PlayerA”(大写字母“P”)。更改this.Notify("playerA");this.Notify("PlayerA");甚至更​​好:

Notify(nameof(PlayerA));

您可以完全摆脱加一个[CallerMemberName]attributeNotify()方法传递帕拉姆的名称。

protected void Notify([CallerMemberName] string propertyName = null)

这可以让你只需要调用Notify()无参数,会自动使用更改属性的名称。

+1

很高兴提及'[CallerMemberName]'! –