在绑定的ObservableCollection中添加或删除项目时更新视图

时间:2013-12-30 05:53:20

标签: c# xaml windows-runtime inotifypropertychanged

我目前正在编写一个使用MVVM的Windows 8.1应用程序。我之所以远离模型只是因为当绑定到View的数据发生变化时,我从未能够正确地更新View。没有多少网站或教程能够解释如何正确使用INotifyPropertyChanged,我现在只是丢失了。我有以下类(包括添加该类型项的方法)。

public class Organization
{
    public Guid Id { get; set; }
    public bool IsShared { get; set; }
    public string Name { get; set; }
    public ObservableCollection<Event> Events { get; set; }

    public async static void Add(string Name)
    {
        Guid Id = Guid.NewGuid();
        string FileName = Id.ToString() + ".txt";
        var Folder = ApplicationData.Current.LocalFolder;

        try
        {
            var Organizations = await Folder.CreateFolderAsync("Organizations", CreationCollisionOption.FailIfExists);
            StorageFile File = await Organizations.CreateFileAsync(FileName, CreationCollisionOption.ReplaceExisting);
            await FileIO.WriteTextAsync(File, JsonConvert.SerializeObject(new Organization { Id = Id, Name = Name, Events=new ObservableCollection<Event>() }));
        }
        catch
        {

        }


    }
}

以下是我的ViewModel:

public class OrganizationsViewModel : Base
{
    private ObservableCollection<Organization> _List = new ObservableCollection<Organization>();
    public ObservableCollection<Organization> List
    {
        get
        {
            Retrieve();
            return _List;
        }
        set
        {

        }
    }

    public async void Retrieve()
    {
        var Folder = ApplicationData.Current.LocalFolder;
        try
        {
            StorageFolder Organizations = await Folder.GetFolderAsync("Organizations");
            var List = await Organizations.GetFilesAsync();

            foreach (StorageFile i in List)
            {
                try
                {
                    using (Stream s = await i.OpenStreamForReadAsync())
                    {
                        using (StreamReader sr = new StreamReader(s))
                        {
                            var item = JsonConvert.DeserializeObject<Organization>(await sr.ReadToEndAsync());
                            _List.Add(item);
                        }
                    }
                }
                catch
                {

                }
            }
        }

        catch
        {

        }
    }
}

基地是:

public class Base : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            // property changed
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

我的ViewModel和/或我的类定义中需要哪些代码才能允许View在我的ObservableCollection中添加或删除项目时正确更新,更重要的是为什么给定的代码有效?提前谢谢!

1 个答案:

答案 0 :(得分:1)

使用数据绑定视图将自动更新,只要它通知它绑定的属性已更改。因此,只要绑定源属性值发生更改,您需要提升属性更改事件。例如:

public class OrganizationsViewModel : Base
{
    private ObservableCollection<Organization> _List = new ObservableCollection<Organization>();
    public ObservableCollection<Organization> List
    {
        get
        {
            Retrieve();
            return _List;
        }
        set
        {
            if(_List != value)
            {
                _List = value;
                NotifyPropertyChanged("List");
            }
        }
    }

    ...
    ...
}

但是,ObservableCollection应该在项目中添加或删除项目时自动通知View,而无需手动引发事件。所以我不能100%确定代码中的问题在哪里。只需尝试在每个属性的setter上调用NotifyPropertyChanged,看看问题是否已解决。至少你现在知道如何使用INotifyPropertyChanged:)