using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Threading.Tasks; using Avalonia.Controls; using Avalonia.Input; namespace DragAndDropSample; public partial class MainWindow : Window { private const string CustomFormat = "application/xxx-avalonia-controlcatalog-custom"; class ItemListBox { public string Name { get; set; } } private List right_listbox = [ new ItemListBox { Name = "123" }, new ItemListBox { Name = "321" }, new ItemListBox { Name = "222" }, new ItemListBox { Name = "555" } ]; private ObservableCollection left_listbox = [ new ItemListBox { Name = "666" }, new ItemListBox { Name = "777" }, new ItemListBox { Name = "111" }, new ItemListBox { Name = "333" } ]; public MainWindow() { InitializeComponent(); RightBox.ItemsSource = right_listbox; LeftBox.ItemsSource = left_listbox; DnDSetup( d => { d.Set(CustomFormat, RightBox.SelectedItem as ItemListBox ?? throw new InvalidOperationException()); return Task.CompletedTask; }, DragDropEffects.Move); } private void DnDSetup(Func factory, DragDropEffects effects) { async void DoDrag(object sender, PointerPressedEventArgs e) { var dragData = new DataObject(); await factory(dragData); var result = await DragDrop.DoDragDrop(e, dragData, effects); } async void Drop(object sender, DragEventArgs e) { var sample = e.Data as DataObject; left_listbox.Add(sample.Get(CustomFormat) as ItemListBox); } RightBox.PointerPressed += DoDrag; AddHandler(DragDrop.DropEvent, Drop); } }