private async void ListView_Drop()

in XamlControlsGallery/ControlPages/ListViewPage.xaml.cs [136:219]


        private async void ListView_Drop(object sender, DragEventArgs e)
        {
            ListView target = (ListView)sender;

            if (e.DataView.Contains(StandardDataFormats.Text))
            {
                DragOperationDeferral def = e.GetDeferral();
                string s = await e.DataView.GetTextAsync();
                string[] items = s.Split('\n');
                foreach (string item in items)
                {

                    // Create Contact object from string, add to existing target ListView
                    string[] info = item.Split(" ", 3);
                    Contact temp = new Contact(info[0], info[1], info[2]);

                    // Find the insertion index:
                    Windows.Foundation.Point pos = e.GetPosition(target.ItemsPanelRoot);

                    // If the target ListView has items in it, use the heigh of the first item
                    //      to find the insertion index.
                    int index = 0;
                    if (target.Items.Count != 0)
                    {
                        // Get a reference to the first item in the ListView
                        ListViewItem sampleItem = (ListViewItem)target.ContainerFromIndex(0);

                        // Adjust itemHeight for margins
                        double itemHeight = sampleItem.ActualHeight + sampleItem.Margin.Top + sampleItem.Margin.Bottom;

                        // Find index based on dividing number of items by height of each item
                        index = Math.Min(target.Items.Count - 1, (int)(pos.Y / itemHeight));

                        // Find the item being dropped on top of.
                        ListViewItem targetItem = (ListViewItem)target.ContainerFromIndex(index);

                        // If the drop position is more than half-way down the item being dropped on
                        //      top of, increment the insertion index so the dropped item is inserted
                        //      below instead of above the item being dropped on top of.
                        Windows.Foundation.Point positionInItem = e.GetPosition(targetItem);
                        if (positionInItem.Y > itemHeight / 2)
                        {
                            index++;
                        }

                        // Don't go out of bounds
                        index = Math.Min(target.Items.Count, index);
                    }
                    // Only other case is if the target ListView has no items (the dropped item will be
                    //      the first). In that case, the insertion index will remain zero.

                    // Find correct source list
                    if (target.Name == "DragDropListView")
                    {
                        // Find the ItemsSource for the target ListView and insert
                        contacts1.Insert(index, temp);
                        //Go through source list and remove the items that are being moved
                        foreach (Contact contact in DragDropListView2.Items)
                        {
                            if (contact.FirstName == temp.FirstName && contact.LastName == temp.LastName && contact.Company == temp.Company)
                            {
                                contacts2.Remove(contact);
                                break;
                            }
                        }
                    }
                    else if (target.Name == "DragDropListView2")
                    {
                        contacts2.Insert(index, temp);
                        foreach (Contact contact in DragDropListView.Items)
                        {
                            if (contact.FirstName == temp.FirstName && contact.LastName == temp.LastName && contact.Company == temp.Company)
                            {
                                contacts1.Remove(contact);
                                break;
                            }
                        }
                    }
                }

                e.AcceptedOperation = DataPackageOperation.Move;
                def.Complete();
            }
        }