2016-09-06 11 views
0

私はxamarinフォームを使用してモバイルアプリケーションを開発しています。私はオブジェクトのリストを持っています。私はリストに行を追加し、これを使用してプロパティをraiseするOnPropertyChangedそしてアイテムを保存した後、私はオブジェクトプロパティのリストのステータスを更新したい。私たちは、Statusプロパティを更新することができますどのように、ここに私のコード例で、コードをチェックして、私を更新してください、ありがとう: - :xamarinフォームのクラスプロパティを更新します。

class Test : INotifyPropertyChanged 
    { 
     public int ID { get; set; } 
     public string Name { get; set; } 

     private bool _status; 
     public bool Status 
     { 
      get { return _status; } 
      set 
      { 
       _status = value; 
       RaisePropertyChanged(); 
      } 
     } 

     #region INotifyPropertyChanged implementation 

     public event PropertyChangedEventHandler PropertyChanged; 

     private void RaisePropertyChanged([CallerMemberName]string propertyName = "") 
     { 
      Volatile.Read(ref PropertyChanged)?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
     } 

     #endregion 
    } 

をそして、あなたが正しい持ちの場合

class Test 
    { 
     public int ID{ get; set; } 
     public string Name { get; set; } 
     public bool Status { get; set; } 
    } 
    class Consume : BaseViewModel 
    { 
     void main() 
     { 
      ObservableCollection<Test> coll = new ObservableCollection<Test>(); 
      coll = await db.GetData(); 

      foreach (var item in coll) 
      { 
       item.Status = true; 
       //How we can update Status property of class 
       OnPropertyChanged("Status"); 
      } 
     } 
    } 

答えて

1

があなたのTestクラスでINotifyPropertyChangedを実装バインディング、item.Status = true;の後にUIがこのプロパティの変更を取得します。

+0

はい、私は、BaseViewModelでINotifyPropertyChangedを実装しました。このメソッド** OnPropertyChanged( "Status"); **を使用してプロパティを更新しましたが、ステータスは更新されません。 –

+1

'Test'と' BaseViewModel'は異なるクラスです。ビューモデルから、ビューモデルに配置されたプロパティを更新できます。 'Status'を' Test'から更新するには 'Test'クラスに' INotifyPropertyChanged'を実装するべきです。 –

+0

ありがとう@Egor Gromadskiy –

関連する問題