using System.Collections.ObjectModel; using DragNDrop.Model; namespace DragNDrop; public partial class OrganizeNounoursPage : ContentPage { public ObservableCollection NounoursCollection { get; set; } = new ObservableCollection() { new Nounours(42, "Chewbacca"), new Nounours(43, "Yoda"), new Nounours(44, "Ewok"), new Nounours(45, "Jabba the Hutt"), new Nounours(666, "Chucky") }; public OrganizeNounoursPage() { InitializeComponent(); BindingContext = this; } private void OnDragStarting(object sender, DragStartingEventArgs e) { e.Data.Properties["value"] = (sender as Element)?.Parent.BindingContext; } private void OnDrop(object sender, DropEventArgs e) { var receivingElement = (sender as Element)?.Parent.BindingContext as Nounours; var draggedElement = e.Data.Properties["value"] as Nounours; int draggedIndex = NounoursCollection.IndexOf(draggedElement); if (NounoursCollection.Contains(draggedElement)) { NounoursCollection.RemoveAt(draggedIndex); } int receivingIndex = NounoursCollection.IndexOf(receivingElement); if (receivingIndex >= draggedIndex) { NounoursCollection.Insert(receivingIndex + 1, draggedElement); } else { NounoursCollection.Insert(receivingIndex, draggedElement); } } }