【问题标题】:Updating the View when items are added or removed from a bound ObservableCollection在绑定的 ObservableCollection 中添加或删除项目时更新视图
【发布时间】:2013-12-30 05:53:19
【问题描述】:

我目前正在编写一个使用 MVVM 的 Windows 8.1 应用程序。我一直远离模型只是因为当绑定到视图的数据发生更改时,我永远无法正确更新视图。没有多少网站或教程能够解释如何正确使用 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 和/或我的类定义中需要哪些代码,以便在我的 ObservableCollection 添加或删除项目时正确更新视图,更重要的是,为什么给定的代码可以工作?提前致谢!

【问题讨论】:

    标签: c# xaml windows-runtime inotifypropertychanged


    【解决方案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% 确定您的代码中的问题出在哪里。只需尝试在每个属性的设置器上调用NotifyPropertyChanged,看看问题是否已解决。至少你现在知道如何使用INotifyPropertyChanged :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-15
      • 2019-04-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-24
      相关资源
      最近更新 更多