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.
mchSamples-MAUI/ch08_HotStuff/DragNDrop/OrganizeNounoursPage.xaml.cs

51 lines
1.4 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)?.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);
}
}
}