public static Rect CalculateDropMarker(Panel panel, UIElementCollection children, Orientation orientation, int dropIndex) { Rect DropMarker; if (children.Count > 0) { if (dropIndex == 0) { Rect ItemRect = children[0].TransformToVisual(panel).TransformBounds(VisualTreeHelperEx.GetBounds(children[0])); DropMarker = orientation == Orientation.Vertical ? new Rect(ItemRect.Left, ItemRect.Top, ItemRect.Width, 0) : new Rect(ItemRect.Left, ItemRect.Top, 0, ItemRect.Height); } else { Rect ItemRect = children[dropIndex - 1].TransformToVisual(panel).TransformBounds(VisualTreeHelperEx.GetBounds(children[dropIndex - 1])); DropMarker = orientation == Orientation.Vertical ? new Rect(ItemRect.Left, ItemRect.Bottom, ItemRect.Width, 0) : new Rect(ItemRect.Right, ItemRect.Top, 0, ItemRect.Height); } } else { DropMarker = new Rect(); } return(DropMarker); }
public static int CalculateDropIndex(Panel panel, UIElementCollection children, Orientation orientation, Point position) { HitTestResult HitTestResult = VisualTreeHelper.HitTest(panel, position); DependencyObject HitItem; if (HitTestResult != null) { DependencyObject[] Ancestors = VisualTreeHelperEx.GetVisualAncestors(HitTestResult.VisualHit, panel); HitItem = Ancestors.Length >= 1 ? Ancestors[1] : null; } else { HitItem = null; } if (HitItem is UIElement) { int ItemIndex = children.IndexOf((UIElement)HitItem); Rect Bounds = ((Visual)HitItem).TransformToAncestor(panel).TransformBounds(VisualTreeHelperEx.GetBounds((Visual)HitItem)); Rect FirstHalfBounds = orientation == Orientation.Vertical ? new Rect(Bounds.Left, Bounds.Top, Bounds.Width, Bounds.Height / 2) : new Rect(Bounds.Left, Bounds.Top, Bounds.Width / 2, Bounds.Height); if (FirstHalfBounds.Contains(position)) { return(ItemIndex); } else { return(ItemIndex + 1); } } else { // Hit cannot be processed or is on the panel -> return index to insert at the end return(children.Count); } }