2016-09-06 32 views
0

我正在使用xamarin表單的移動應用程序,我有一個對象列表。我已經添加列表中的行,並使用此OnPropertyChanged並提高屬性保存項目後,我想更新對象屬性列表的狀態。我們怎樣才能更新狀態屬性,這裏是我的代碼示例,請檢查代碼並更新我,謝謝: -更新xamarin表格中的類屬性

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

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 
    } 

如果你有正確的綁定,在item.Status = true;之後UI將獲得此屬性的更改。

+0

,是的,我已經在BaseViewModel中實現了INotifyPropertyChanged。以便我使用此方法更新屬性** OnPropertyChanged(「狀態」); **但未更新狀態。 –

+1

'Test'和'BaseViewModel'是不同的類。從視圖模型中,您可以更新放置在視圖模型中的屬性。您應該在'Test'類中實現'INotifyPropertyChanged'來更新'Test'中的'Status'。 –

+0

謝謝@Egor Gromadskiy –