Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove item from collection throws exception #12

Open
kurin123 opened this issue Apr 25, 2016 · 5 comments
Open

Remove item from collection throws exception #12

kurin123 opened this issue Apr 25, 2016 · 5 comments

Comments

@kurin123
Copy link

kurin123 commented Apr 25, 2016

When I try to remove item from original collection I'm getting exception:
"Collection Remove event must specify item position."

        private ObservableCollection<int> oc = new ObservableCollection<int>();
        private ObservableView<int> ov;

        oc.Add(10);
        oc.Add(20);
        oc.Add(30);

        ov = oc.AsObservableQuery().Where(i => i < 30).AsObservableQuery().ToObservableView();

        oc.Remove(20);

Managed to get around with new methods:

        public static void RaiseRemoveEvent<T>(Action<NotifyCollectionChangedEventArgs> raise, IEnumerable<T> oldItems, int oldIdx)
        {
#if !SILVERLIGHT && !PCL
            raise(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, oldItems.ToList(), oldIdx));
#else
            foreach (var item in oldItems)
                raise(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, oldIdx));
#endif
        }

        public static void RaiseRemoveEvent<T>(Action<NotifyCollectionChangedEventArgs> raise, T oldItem, int oldIdx)
        {
#if !SILVERLIGHT && !PCL
            raise(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, oldItem, oldIdx));
#else
            raise(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, oldItem, oldIdx));
#endif
        }

And in WhereOperation calling
NotifyCollectionChangedUtil.RaiseRemoveEvent(OnCollectionChanged, oldItems**, args.OldStartingIndex**);

@wasabii
Copy link
Owner

wasabii commented Apr 25, 2016

What package are you using this on? Silverlight or the PCL one? Your code example seems to work fine with full .Net

@kurin123
Copy link
Author

WPF .NET 4.5. The exception occurs when I put ObservableView as ItemsSource for ItemsControl.

            ov = oc.AsObservableQuery().Where(i => i < 40).AsObservableQuery().ToObservableView();

            itemsControl.ItemsSource = ov; //without this it works OK, haven't notice that before

            oc.Remove(20);

@wasabii
Copy link
Owner

wasabii commented Apr 26, 2016

Okay. Pinned down. Looks like I never implemented any sort of index space translation in the WhereOperation. And for whatever reason you hit a circumstance that I never tested. Which seems weird as this is fairly obvious. This is going to be a pretty hard problem to solve properly all around.

Your solution allows items to come and go from the underlying collection, and then the item index that was removed from the underlying collection gets passed through unchanged into the Where operation, and then out of the Where operation.

This doesn't work if the Where operation is actually removing objects. Because if so, the index of the items being filtered won't match the index of the items that are returned from the filter. I'll see if I can come up with some sort of solution.

@wasabii
Copy link
Owner

wasabii commented Apr 26, 2016

So, a temporary option for you might be to turn your query into an ObservableBuffer by invoking .ToBuffer() on the ObservableView.

This basically wraps the ObservableView in a collection that maintains a final list of all the results, and updates it in response to events in the ObservableView, instead of simply proxying collection changed events. That final collection will raise events with indexes properly.

@kurin123
Copy link
Author

Thanks for help. ToBuffer() solved it for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants