2016-05-26 5 views
2

私は今wpfを学んでいますが、コーディングの際に問題があります。プレイデータはMainWindowから取得し、プレイヤーのID、名前を表示します....しかし、私はプレイヤーの情報を更新する必要があります。 SubWindowViewModel側で、更新バインディングプロパティがありますが、問題があります。ビュー側でプロパティを更新できません.ViewModelのプロパティが変更されたときにSubWindowを更新したいです。ビューモデルのプロパティのサブウィンドウを更新する方法が変更されましたか?

public SubWindow(Player player) 
    { 
     InitializeComponent(); 
     ISubWindowViewModel subWindowViewModel = new SubWindowViewModel(); 
     #region Get data 
     subWindowViewModel.ID = player.ID; 
     subWindowViewModel.Name = player.Name; 
     subWindowViewModel.Sex = player.Sex; 
     #endregion 
     this.DataContext = subWindowViewModel; 
    } 

とビューモデルはxaml.csで、INotifyPropertyChangedの実装を持っている:

<TextBox x:Name="Name" Text="{Binding UserName,Mode=TwoWay}"/> 
    <TextBox x:Name="Sex" Text="{Binding Sex,Mode=TwoWay}" /> 
    <TextBox x:Name="ID" Text="{Binding ID,Mode=TwoWay}"/> 

はどうもありがとうございました!

+0

あなたのビューモデルコードの見込みはありますか? –

+0

申し訳ありませんが、いくつかのエラーコードがあります。ISubWindowViewModelに起因するかどうかはわかりません。SubWindowViewModel = new SubWindowViewModel(); SubWindowViewModelはINotifyPropertyChangedを使用しています。 ISubWindowViewModelがINotifyPropertyChangedを継承していない問題がありますか?申し訳ありませんが、私はwpfに新しく、ありがとうございます – Dota2Player

+0

プロパティが設定されているときに、OnPropertyChanged()メソッドを呼び出しましたか? そして、この更新部分すべてにmainWindow、viewModelクラスなどを含むいくつかのコードを追加することができます。だから、私たちは皆もっと明確にすることができました。 – Tops

答えて

2

私はプロコーダーでもありません。私が何を考えて

あなたはのviewmodelクラスでインターフェイスをINotifyPropertyChangedのと呼ばれるものを実装する必要があります。

リンクを確認してください。そこにもっと多くのリンクがあるかもしれません。 How to: Implement Property Change Notification

INotifyPropertyChanged Interface in WPF with Example

学び、それを実装。それが役に立てば幸い。ありがとうございました。

EDIT: 私はPersonViewModelとしてあなたviewModelClass名を想定しています。あなたのビューモデルクラスはsthです。以下のように。

class PersonViewModel:INotifyPropertyChanged 
{ 
    private string _username; 

    public string UserName 
    { 
     get { return _username; } 
     set { 
      _username= value; 
      OnPropertyChanged("UserName"); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    public void OnPropertyChanged(string Property) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(Property)); 
     } 
    } 
} 

あなたのxamlは正しいです。だから私はあなたが同じを渡したと仮定していますviewmodelclassオブジェクト(コンストラクタで)は、MainWindowで使用している。したがって、コードの背後には、上記のようにウィンドウのDataContextを設定する必要があります。

public SubWindow(PlayerViewModel player) 
{ 
    InitializeComponent(); 
    this.DataContext=player; 
} 
+0

本当にコメントでなければなりません。答えではありません。 – niksofteng

+0

しかし、ありがとうございます、viewmodelクラスのINotifyPropertyChangedインターフェイスはviewmodelで実装されていますが、Inのビュー、ISubWindowViewModelを使用している可能性があります。SubWindowViewModel = new SubWindowViewModel();助言はありますか?ありがとう – Dota2Player

関連する問題