2017-11-18 219 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]'! –