/// <summary> /// Determine the proper insertion index /// </summary> /// <param name="itemsControl"></param> /// <param name="e"></param> /// <returns></returns> private static int FindInsertionIndex(ItemsControl itemsControl, DragEventArgs e) { UIElement dropTargetContainer = DragUtilities.GetItemContainerFromPoint(itemsControl, e.GetPosition(itemsControl)); if (dropTargetContainer != null) { int index = itemsControl.ItemContainerGenerator.IndexFromContainer(dropTargetContainer); return(DragUtilities.IsPointInTopHalf(itemsControl, e) ? index : index + 1); } return(itemsControl.Items.Count); }
/// <summary> /// Initialize the insertion point marker /// </summary> /// <param name="itemsControl"></param> /// <param name="e"></param> private void InitializeInsertAdorner(ItemsControl itemsControl, DragEventArgs e) { if (this._insertAdorner == null && this.ShowInsertAdorner) { UIElement itemContainer = DragUtilities.GetItemContainerFromPoint(itemsControl, e.GetPosition(itemsControl)); if (itemContainer != null) { this._insertAdorner = new InsertAdorner(DragUtilities.IsPointInTopHalf(itemsControl, e), DragUtilities.IsItemControlOrientationHorizontal(itemsControl), itemContainer, AdornerLayer.GetAdornerLayer(itemsControl), itemsControl); } } }
/// <summary> /// This starts the drag operation from the given ItemsControl /// </summary> /// <param name="itemsControl"></param> private void DragStarted(ItemsControl itemsControl) { DragDropEffects allowedEffects = DragDropEffects.Copy | DragDropEffects.Move; if (this.DragInitiated != null) { var dde = new DragDropEventArgs(itemsControl, null, this._data) { AllowedEffects = allowedEffects }; this.DragInitiated(this, dde); if (dde.Cancel) { this.ResetState(); return; } allowedEffects = dde.AllowedEffects; } UIElement draggedItemContainer = DragUtilities.GetItemContainerFromPoint(itemsControl, this._dragStartPosition); this._isDragging = true; DataObject dObject = new DataObject(this.ItemTypeKey, new DragInfo { Data = this._data, Source = itemsControl, AllowOnlySelf = this.AllowOnlySelf }); DragDropEffects e = DragDrop.DoDragDrop(itemsControl, dObject, allowedEffects); if ((e & DragDropEffects.Move) != 0) { if (draggedItemContainer != null) { int dragItemIndex = itemsControl.ItemContainerGenerator.IndexFromContainer(draggedItemContainer); DragUtilities.RemoveItem(itemsControl, dragItemIndex); } else { DragUtilities.RemoveItem(itemsControl, this._data); } } this.ResetState(); }