locked
NotifyCollectionChangedAction.Remove -- Removes too many rows

    Question

  • I'm working on building a virtualized data class. That way I only have a limited number of rows from my database in memory at one time in combination with the UI virtualization built into win8. I started off with an async class that was read-only for WPF and converted some of it over. However, I'm looking to upgrade it to be able to add/remove items from its list. I have a demo which uses the simplistic customer class (properties: Id, Title) to try and get the class into working form, however I seem to have much trouble when I update the UI. The class itself seems to function as it should. I can remove items from the list without issue, however it seems that once I try to update the UI I generate problems. I believe I've isolated the issue to something I'm doing wrong when I call the remove notification through INotifyCollectionChanged. Here is the steps I take to generate the error:

    1. I load data on startup.
    2. I click a UI button to remove the first row (which is successful and the UI updates appropriately).
    3. I click the UI button to remove again (which fails because now both the new first and second row are removed, i.e. it removed the original 2,3 rows).

    If I were to refresh the collection, it reinserts the extra deleted row, hence something is going wrong with the Removal call.
    I've added breakpoints and it appears that the Removal call is only called once, hence I can't figure out why it removes two rows. When it does remove two rows, it seems to do them at the same time as opposed to one and then the other.

    This is my UI remove call:

    OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, new object(), 0));

    I used to put in the actual object for the old data item, but that didn't solve problems so now I just put in a generic object. I am not able to call Remove without including the index otherwise I would just use the old data item without the index.

    I can provide sample code if someone is interested in trying to reproduce the issue on their end.

    Thank you for your help!

    Jeremy


    • Edited by JGSweets Monday, August 25, 2014 7:18 PM clarifcation
    Monday, August 25, 2014 5:49 PM

Answers

  • Well, I fortunately found my problem. When I was calling the handler I mistakenly tried to make some adjustments to it, but really I shouldn't have done anything at all. By removing the following code I fixed my own problem:

    event NotifyCollectionChangedEventHandler INotifyCollectionChanged.CollectionChanged
            {
                add { CollectionChanged += value; }
                remove { CollectionChanged += value; }
            }

    Sorry for troubling you and hopefully this helps others who might make the same mistake :)
    • Marked as answer by JGSweets Monday, August 25, 2014 7:37 PM
    Monday, August 25, 2014 7:37 PM