簡體   English   中英

在從綁定的ObservableCollection中添加或刪除項目時更新視圖

[英]Updating the View when items are added or removed from a bound ObservableCollection

我目前正在編寫使用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和/或類定義中使用哪些代碼,以便在從ObservableCollection中添加或刪除項時正確地更新View,更重要的是為什么給定的代碼可以正常工作? 提前致謝!

使用數據綁定,只要View通知它綁定到的屬性已更改,它將自動更新。 因此,您需要的是在綁定源屬性值更改時引發屬性更改事件。 例如 :

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 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM