ObservableCollection无法使用RaisePropertyChangeEvent

时间:2016-07-19 06:07:54

标签: windows-phone-8 mvvm windows-phone-8.1 mvvm-light observablecollection

我正在使用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");
    }
}

1 个答案:

答案 0 :(得分:0)

尝试在AddNewPeople方法

中执行此操作
public void AddNewPeople()
{
       ProfileList.Clear();

       ProfileList.Add(new ViewPeopleModel{ ImageURL ="url5" });
       ProfileList.Add(new ViewPeopleModel{ ImageURL ="url6" });
}