I have following ListView
<ListView Name="listAccounts" Width="300" AllowDrop="True" SelectionMode="Single" CanDragItems="True" CanDrag="True" CanReorderItems="True" Background="{ThemeResource myBackground}" DragItemsStarting="listAccounts_DragItemsStarting" Drop="listAccounts_Drop">
and defined my event handlers as
private void listAccounts_DragItemsStarting(object sender, DragItemsStartingEventArgs e)
{
e.Data.SetData("itemIndex", (e.Items[0] as AccountList).Text.ToString());
}
private async void listAccounts_Drop(object sender, DragEventArgs e)
{
string itemIndexString = await e.Data.GetView().GetTextAsync("itemIndex");
}
I don't know what else I can do. I want to realize the movement of the list items in the same list.
I went over to the official Windows 10 samples, looked for the drag-drop sample and trimmed it down (like removing the drag from target to source). Turns out you don't even have to handle any events to make re-ordering in a single
ListView
work.Check your
ObservableCollection
after reordering items and you'll notice it's correct. If you want to track the re-ordering, you'll have to check theCollectionChanged
event on yourObservableCollection
, as there is no event on theListView
to do this.If you want to support drag & drop accross multilple listviews, I'd say have another look at the sample.