每当更改其源属性时,我的表视图都不会更新。代码如下:
public override void ViewDidLoad()
{
base.ViewDidLoad();
viewmodel = this.ViewModel as ListViewModel;
viewmodel.PropertyChanged += HandlePropertyChangedEventHandler;;
var source = new MvxSimpleTableViewSource( TableView, LaborCell.Key, LaborCell.Key);
TableView.Source = source;
var set = this.CreateBindingSet<ListView, ListViewModel>();
set.Bind(source).To(vm => vm.LaborTransactions);
set.Apply();
TableView.ReloadData();
}
ViewModel:
public class ListViewModel :MaxRawBaseViewModel
{
public ListViewModel():base()
{
LoadLaborTransactions();
}
private Collection<LaborTransaction> _laborTransactions;
public Collection<LaborTransaction> LaborTransactions
{
get { return _laborTransactions; }
}
public void LoadLaborTransactions()
{
_laborTransactions = DataService.GetLaborTransactions(somenumber);
RaisePropertyChanged(() => LaborTransactions);
}
}
每当
Transactions
中的更改在propertychanged方法上调用tablview.reolad()
时。但是它没有重新加载我的tableviewvoid HandlePropertyChangedEventHandler(object sender, System.ComponentModel.PropertyChangedEventArgs e){
if (e.PropertyName.Equals("LaborTransactions"))
{
TableView.ReloadData();
}
}
最佳答案
Collection<T>
不实现INotifyPropertyChanged
。您可以在文档here中进行验证。您需要将LaborTransactions
属性更改为实现INotifyPropertyChanged
的集合类型,例如ObservableCollection<T>
和MvxObservableCollection<T>
。您可以看到ObservableCollection<T>
实现了INotifyPropertyChanged
here
更改您的LaborTransactions
这样:
private ObservableCollection<LaborTransaction> _laborTransactions;
public ObservableCollection<LaborTransaction> LaborTransactions
{
get { return _laborTransactions; }
set {
return _laborTransactions;
RaisePropertyChanged(() => LaborTransactions);
}
}