Well, that’s pretty ugly, but I didn’t find another way, not myself, and also did not search the net for answers. There was another deadline at Microsoft, which prevented the inclusion of fairly obvious functionality ...
Basically, the method below does everything manually, getting the drag location and checking it for list items that will be used as index links.
private void ListBoxDragDropTarget_Drop(object sender, Microsoft.Windows.DragEventArgs e)
{
if (e.Effects.HasFlag(DragDropEffects.Copy))
{
SelectionCollection selections = ((ItemDragEventArgs)e.Data.GetData("System.Windows.Controls.ItemDragEventArgs")).Data as SelectionCollection;
int? index = null;
if (selections != null)
{
Point p1 = e.GetPosition(this.LayoutRoot);
var elements = VisualTreeHelper.FindElementsInHostCoordinates(p1, this.LayoutRoot);
foreach (var dataItem in this.lbxConfiguration.Items)
{
ListBoxItem lbxItem = this.lbxConfiguration.ItemContainerGenerator.ContainerFromItem(dataItem) as ListBoxItem;
if (elements.Contains(lbxItem))
{
Point p2 = e.GetPosition(lbxItem);
index = this.lbxConfiguration.Items.IndexOf(dataItem);
if (p2.Y > lbxItem.ActualHeight / 2)
index += 1;
break;
}
}
if (index != null)
{
foreach (var selection in selections)
{
(lbxConfiguration.ItemsSource as IList<ViewItem>).Insert((int)index, (selection.Item as ViewItem).Clone());
}
}
}
}
}
source
share