Joe
Joe

Reputation: 11

UWP listview swipe delete issue

I am working on a UWP project and tried to add a feature: swipe left and delete item from listview. I followed the instruction from https://learn.microsoft.com/en-us/windows/uwp/design/controls-and-patterns/swipe#how-does-swipe-work and download the sample code from https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/XamlUIBasics. Basically, I update the SwipePage.xaml.cs file from the github and add more items in the listview items source. When there are more than 25 listview items, if one item is swiped and deleted, the listview randomly pick one listview item and cover it with the delete icon. After you click delete icon and delete that listview item, continue scroll up/down the listview will crash the whole application.

Can anyone help solve this wired issue?

Upvotes: 1

Views: 655

Answers (1)

Johnny Westlake
Johnny Westlake

Reputation: 1460

The original code is flawed.

private void DeleteItem_ItemInvoked(SwipeItem sender, SwipeItemInvokedEventArgs args)
{
    int i = lv.Items.IndexOf(args.SwipeControl.DataContext);
    items.RemoveAt(1);
}

The items.RemoveAt should be removing i, not 1

private void DeleteItem_ItemInvoked(SwipeItem sender, SwipeItemInvokedEventArgs args)
{
    int i = lv.Items.IndexOf(args.SwipeControl.DataContext);
    items.RemoveAt(i);
}

Upvotes: 1

Related Questions