You're making it way too complicated;)
Here is the absolute minimum that is necessary for the simplest implementation of DragDrop.
a.) Make sure the Drop Traget's (here listBox2)
AllowDrop
is set to
true
b.) This will start the DragDrop operation
private void listBox1_MouseMove(object sender, MouseEventArgs e)
{
if (listBox1.SelectedItem == null) return;
if (e.LeftButton == MouseButtonState.Pressed)
{
DragDrop.DoDragDrop(sender as ListBox, listBox1.SelectedItem, DragDropEffects.Copy);
}
}
c.) This will handle the Drop
private void listBox2_Drop(object sender, DragEventArgs e)
{
ListBoxItem item = e.Data.GetData(typeof(ListBoxItem)) as ListBoxItem;
if (item == null) return;
listBox2.Items.Add(item.Content.ToString());
e.Handled = true;
}
For more complex situations where several different DragDrop sources are available you can then fine tune with this
private void listBox2_DragOver(object sender, DragEventArgs e)
{
if (e.Data.GetData(typeof(ListBoxItem)) as ListBoxItem == null)
{
e.Effects = DragDropEffects.None;
}
else
{
e.Effects = DragDropEffects.Copy;
}
}