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)?.BindingContext; } private void OnDrop(object sender, DropEventArgs e) { if ((sender as Element)?.BindingContext is not Nounours receivingElement) return; if (e.Data.Properties["value"] as Nounours is not Nounours draggedElement) return; int draggedIndex = NounoursCollection.IndexOf(draggedElement); if (draggedIndex > -1) { NounoursCollection.RemoveAt(draggedIndex); } int receivingIndex = NounoursCollection.IndexOf(receivingElement); if (receivingIndex == -1) throw new ArgumentNullException("There should be a receiving element"); if (receivingIndex >= draggedIndex) { NounoursCollection.Insert(receivingIndex + 1, draggedElement); } else { NounoursCollection.Insert(receivingIndex, draggedElement); } } }