From 9c4f7b2d9edc06cb52da8573b91248644fe6966b Mon Sep 17 00:00:00 2001 From: neuecc Date: Tue, 10 Sep 2024 01:22:38 +0900 Subject: [PATCH] Dictionary FilteredView add operation adds last --- sandbox/ConsoleApp/Program.cs | 33 ++++--------------- .../SynchronizedViewList.cs | 17 ++++++++-- 2 files changed, 21 insertions(+), 29 deletions(-) diff --git a/sandbox/ConsoleApp/Program.cs b/sandbox/ConsoleApp/Program.cs index f547f51..b293fdf 100644 --- a/sandbox/ConsoleApp/Program.cs +++ b/sandbox/ConsoleApp/Program.cs @@ -7,36 +7,17 @@ using System.Collections.Generic; +var dict = new ObservableDictionary(); +var view = dict.CreateView(x => x).ToNotifyCollectionChanged(); +dict.Add(key: 1, value: "foo"); +dict.Add(key: 2, value: "bar"); - -// Queue <-> List Synchronization -var queue = new ObservableQueue(); - -queue.Enqueue(1); -queue.Enqueue(10); -queue.Enqueue(100); -queue.Enqueue(1000); -queue.Enqueue(10000); - -using var view = queue.CreateView(x => x.ToString() + "$"); - -using var viewList = view.ToViewList(); - -Console.WriteLine(viewList[2]); // 100$ - - -view.ViewChanged += View_ViewChanged; - -void View_ViewChanged(in SynchronizedViewChangedEventArgs eventArgs) +foreach (var item in view) { - if (eventArgs.Action == NotifyCollectionChangedAction.Add) - { - // eventArgs.OldItem.View. - } - - throw new NotImplementedException(); + Console.WriteLine(item); } + class ViewModel { public int Id { get; set; } diff --git a/src/ObservableCollections/SynchronizedViewList.cs b/src/ObservableCollections/SynchronizedViewList.cs index f1dc3a5..418768f 100644 --- a/src/ObservableCollections/SynchronizedViewList.cs +++ b/src/ObservableCollections/SynchronizedViewList.cs @@ -64,9 +64,20 @@ private void Parent_ViewChanged(in SynchronizedViewChangedEventArgs e) case NotifyCollectionChangedAction.Add: // Add or Insert if (e.IsSingleItem) { - var index = listView.Insert(e.NewStartingIndex, e.NewItem.View); - OnCollectionChanged(e.WithNewStartingIndex(index)); - return; + if (e.OldStartingIndex == -1) + { + // add operation + var index = listView.Count; + listView.Insert(index, e.NewItem.View); + OnCollectionChanged(e.WithNewStartingIndex(index)); + return; + } + else + { + var index = listView.Insert(e.NewStartingIndex, e.NewItem.View); + OnCollectionChanged(e.WithNewStartingIndex(index)); + return; + } } else {