我正在使用MVVM灯。最初我添加了两个项目;之后,我在列表中添加了两个新项目,并调用RaisePropertyChanged
事件;但它没有更新列表视图,它只显示旧项目。我的代码有什么问题?
我的模特
public class ViewPeopleModel
{
public event PropertyChangedEventHandler PropertyChanged;
private string _imageURL;
public string ImageURL
{
get
{
return _imageURL;
}
set
{
_imageURL = value;
OnPropertyChanged("ImageURL");
}
}
public void OnPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
查看模型
public class ViewPeopleViewModel : ViewModelBase
{
public ObservableCollection<ViewPeopleModel> ProfileList { get; set; }
public ViewPeopleViewModel()
{
ObservableCollection<ViewPeopleModel> _people = new ObservableCollection<ViewPeopleModel>();
_people.Add(new ViewPeopleModel { ImageURL = "url1" });
_people.Add(new ViewPeopleModel { ImageURL = "url2" });
ProfileList = _people;
RaisePropertyChanged("ProfileList");
}
public void AddNewPeople()
{
ObservableCollection<ViewPeopleModel> _people = new ObservableCollection<ViewPeopleModel>();
_people.Add(new ViewPeopleModel { ImageURL = "url5" });
_people.Add(new ViewPeopleModel { ImageURL = "url6" });
ProfileList = _people;
RaisePropertyChanged("ProfileList");
}
}
答案 0 :(得分:0)
尝试在AddNewPeople
方法
public void AddNewPeople()
{
ProfileList.Clear();
ProfileList.Add(new ViewPeopleModel{ ImageURL ="url5" });
ProfileList.Add(new ViewPeopleModel{ ImageURL ="url6" });
}