2017-11-18 18 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));

あなたは完全にNotify()方法に[CallerMemberName]attributeを追加することにより、PARAMの名前を渡すことを取り除くことができます。

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

これはあなただけでパラメータなしでNotify()を呼び出すことができますし、変更されたプロパティの名前が自動的に使用されます。

+1

'[CallerMemberName]'に言及してうれしい! –

関連する問題