locked
Detect ListView items order changed. RRS feed

  • Question

  • I have turned on CanReorderItems and AllowDrop so that items can be moved in my ListView and now I want to detect this change & update the index in my savefile, I can handle saving using a manual button press but I want it to be automatic...and possibly even know what change occurred so I don't have to iterate through all the items in the ListView to save all over again...or to just see what's changed...

    Is this possible?  I thought the Drop event might help, but it never gets fired even when I grab an item and move it successfully to a different spot...the other events don't seem to get called either, or get called even when I don't grab items or try to move them...so what can I use, or should I be using to do this?

    Wednesday, October 9, 2013 4:28 PM

Answers

  • Hi Michael,

    I think you're using an ObservableCollection<T> as ItemsSource for the ListView, right? :-)

    On the ObservableCollection<T>, install an EventHandler for the CollectionChanged-Event. When you reorder an item via Drag Drop, the item is first removed and in the second step added at a new position to the ObservableCollection. So you can do a check like below in an EventHandler:

           object oldItem = null;
            private async void OnCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
            {
                if (e.Action == NotifyCollectionChangedAction.Remove && e.OldItems.Count == 1)
                {
                    oldItem = e.OldItems[0];
                }
                if(e.Action == NotifyCollectionChangedAction.Add && e.NewItems.Count==1
                    && e.NewItems[0]==oldItem)
                {
                    await new MessageDialog("Reorder happened").ShowAsync();
                }
            }


    Thomas Claudius Huber

    "If you can´t make your app run faster, make it at least look & feel extremly fast"

    twitter: @thomasclaudiush
    homepage: www.thomasclaudiushuber.com
    author of: ultimate Windows Store Apps handbook | ultimate WPF handbook | ultimate Silverlight handbook

    • Marked as answer by Anne Jing Friday, October 18, 2013 2:22 AM
    Wednesday, October 9, 2013 6:26 PM

All replies

  • Hi Michael,

    I think you're using an ObservableCollection<T> as ItemsSource for the ListView, right? :-)

    On the ObservableCollection<T>, install an EventHandler for the CollectionChanged-Event. When you reorder an item via Drag Drop, the item is first removed and in the second step added at a new position to the ObservableCollection. So you can do a check like below in an EventHandler:

           object oldItem = null;
            private async void OnCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
            {
                if (e.Action == NotifyCollectionChangedAction.Remove && e.OldItems.Count == 1)
                {
                    oldItem = e.OldItems[0];
                }
                if(e.Action == NotifyCollectionChangedAction.Add && e.NewItems.Count==1
                    && e.NewItems[0]==oldItem)
                {
                    await new MessageDialog("Reorder happened").ShowAsync();
                }
            }


    Thomas Claudius Huber

    "If you can´t make your app run faster, make it at least look & feel extremly fast"

    twitter: @thomasclaudiush
    homepage: www.thomasclaudiushuber.com
    author of: ultimate Windows Store Apps handbook | ultimate WPF handbook | ultimate Silverlight handbook

    • Marked as answer by Anne Jing Friday, October 18, 2013 2:22 AM
    Wednesday, October 9, 2013 6:26 PM
  • Thanks, although I figured out a different way with the same general outcome using eventhandler ListView1.Items.VectorChanged seems to do the same thing, can you tell me the practical differences between them if any, and which one I should be using generally?  Although I am currently ignoring the delete action in general(by skipping over the 1st event, in which I don't know how to tell what happened, I just know it was a delete event) because the only current action do-able in my program for the items I am handling is a reorder operation, and no delete operations...

    I am also adding the eventhandler after my items are added, because it calls for the initial items being added inside OnNavigatedTo()

    I am going to add deletion, I have addition but it's handled in OnNavigatedTo() where the rest of the info is populated initially...and I guess a simple flag that makes it aware a deletion is about to occur, so it will update correctly in that case when deletion capability is added...likely sometime tomorrow as I have a busy rest of the day...

    Wednesday, October 9, 2013 7:17 PM