private void UpdateDropTarget(double dragItemOffset, bool showTransition)
{
Point dragPoint = ReorderListBox.GetHostCoordinates(
new Point(this.dragInterceptorRect.Left, this.dragInterceptorRect.Top + dragItemOffset));
IEnumerable<UIElement> targetElements = VisualTreeHelper.FindElementsInHostCoordinates(dragPoint, this.itemsPanel);
ReorderListBoxItem targetItem = targetElements.OfType<ReorderListBoxItem>().FirstOrDefault();
if (targetItem != null)
{
GeneralTransform targetTransform = targetItem.DragHandle.TransformToVisual(this.dragInterceptor);
Rect targetRect = targetTransform.TransformBounds(new Rect(new Point(0, 0), targetItem.DragHandle.RenderSize));
double targetCenter = (targetRect.Top + targetRect.Bottom) / 2;
int targetIndex = this.itemsPanel.Children.IndexOf(targetItem);
int childrenCount = this.itemsPanel.Children.Count;
bool after = dragItemOffset > targetCenter;
ReorderListBoxItem indicatorItem = null;
if (!after && targetIndex > 0)
{
ReorderListBoxItem previousItem = (ReorderListBoxItem)this.itemsPanel.Children[targetIndex - 1];
if (previousItem.Tag as string == ReorderListBoxItem.DropAfterIndicatorState)
{
indicatorItem = previousItem;
}
}
else if (after && targetIndex < childrenCount - 1)
{
ReorderListBoxItem nextItem = (ReorderListBoxItem)this.itemsPanel.Children[targetIndex + 1];
if (nextItem.Tag as string == ReorderListBoxItem.DropBeforeIndicatorState)
{
indicatorItem = nextItem;
}
}
if (indicatorItem == null)
{
targetItem.DropIndicatorHeight = this.dragIndicator.Height;
string dropIndicatorState = after ?
ReorderListBoxItem.DropAfterIndicatorState : ReorderListBoxItem.DropBeforeIndicatorState;
VisualStateManager.GoToState(targetItem, dropIndicatorState, showTransition);
targetItem.Tag = dropIndicatorState;
indicatorItem = targetItem;
}
for (int i = targetIndex - 5; i <= targetIndex + 5; i++)
{
if (i >= 0 && i < childrenCount)
{
ReorderListBoxItem nearbyItem = (ReorderListBoxItem)this.itemsPanel.Children[i];
if (nearbyItem != indicatorItem)
{
VisualStateManager.GoToState(nearbyItem, ReorderListBoxItem.NoDropIndicatorState, showTransition);
nearbyItem.Tag = ReorderListBoxItem.NoDropIndicatorState;
}
}
}
this.UpdateDropTargetIndex(targetItem, after);
}
}