You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.3 KiB
52 lines
1.3 KiB
using System.Collections.ObjectModel;
|
|
using DragNDrop.Model;
|
|
|
|
namespace DragNDrop;
|
|
|
|
public partial class OrganizeNounoursPage : ContentPage
|
|
{
|
|
public ObservableCollection<Nounours> NounoursCollection { get; set; } = new ObservableCollection<Nounours>()
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
} |