private static void DropTargetOnDragOver(object sender, DragEventArgs e, EventType eventType) { var elementPosition = e.GetPosition((IInputElement)sender); var dragInfo = m_DragInfo; var dropInfo = new DropInfo(sender, e, dragInfo, eventType); var dropHandler = TryGetDropHandler(dropInfo, sender as UIElement); var itemsControl = dropInfo.VisualTarget; dropHandler.DragOver(dropInfo); if (DragAdorner == null && dragInfo != null) { CreateDragAdorner(dropInfo); } DragAdorner?.Move(e.GetPosition(DragAdorner.AdornedElement), dragInfo != null ? GetDragMouseAnchorPoint(dragInfo.VisualSource) : default(Point), ref _adornerMousePosition, ref _adornerSize); Scroll(dropInfo, e); if (HitTestUtilities.HitTest4Type <ScrollBar>(sender, elementPosition) || HitTestUtilities.HitTest4GridViewColumnHeader(sender, elementPosition) || HitTestUtilities.HitTest4DataGridTypesOnDragOver(sender, elementPosition)) { e.Effects = DragDropEffects.None; e.Handled = true; return; } // If the target is an ItemsControl then update the drop target adorner. if (itemsControl != null) { // Display the adorner in the control's ItemsPresenter. If there is no // ItemsPresenter provided by the style, try getting hold of a // ScrollContentPresenter and using that. UIElement adornedElement = null; if (itemsControl is TabControl) { adornedElement = itemsControl.GetVisualDescendent <TabPanel>(); } else if (itemsControl is DataGrid || (itemsControl as ListView)?.View is GridView) { adornedElement = itemsControl.GetVisualDescendent <ScrollContentPresenter>() as UIElement ?? itemsControl.GetVisualDescendent <ItemsPresenter>() as UIElement ?? itemsControl; } else { adornedElement = itemsControl.GetVisualDescendent <ItemsPresenter>() as UIElement ?? itemsControl.GetVisualDescendent <ScrollContentPresenter>() as UIElement ?? itemsControl; } if (adornedElement != null) { if (dropInfo.DropTargetAdorner == null) { DropTargetAdorner = null; } else if (!dropInfo.DropTargetAdorner.IsInstanceOfType(DropTargetAdorner)) { DropTargetAdorner = DropTargetAdorner.Create(dropInfo.DropTargetAdorner, adornedElement, dropInfo); } var adorner = DropTargetAdorner; if (adorner != null) { var adornerBrush = GetDropTargetAdornerBrush(dropInfo.VisualTarget); if (adornerBrush != null) { adorner.Pen.Brush = adornerBrush; } adorner.DropInfo = dropInfo; adorner.InvalidateVisual(); } } } // Set the drag effect adorner if there is one if (dragInfo != null && (EffectAdorner == null || EffectAdorner.Effects != dropInfo.Effects)) { CreateEffectAdorner(dropInfo); } EffectAdorner?.Move(e.GetPosition(EffectAdorner.AdornedElement), default(Point), ref _effectAdornerMousePosition, ref _effectAdornerSize); e.Effects = dropInfo.Effects; e.Handled = !dropInfo.NotHandled; if (!dropInfo.IsSameDragDropContextAsSource) { e.Effects = DragDropEffects.None; } }
private static void CreateDragAdorner(DropInfo dropInfo) { var dragInfo = dropInfo.DragInfo; var template = GetDropAdornerTemplate(dropInfo.VisualTarget) ?? GetDragAdornerTemplate(dragInfo.VisualSource); var templateSelector = GetDropAdornerTemplateSelector(dropInfo.VisualTarget) ?? GetDragAdornerTemplateSelector(dragInfo.VisualSource); UIElement adornment = null; var useDefaultDragAdorner = template == null && templateSelector == null && GetUseDefaultDragAdorner(dragInfo.VisualSource); var useVisualSourceItemSizeForDragAdorner = GetUseVisualSourceItemSizeForDragAdorner(dragInfo.VisualSource); if (useDefaultDragAdorner) { template = dragInfo.VisualSourceItem.GetCaptureScreenDataTemplate(dragInfo.VisualSourceFlowDirection); } if (template != null || templateSelector != null) { if (dragInfo.Data is IEnumerable && !(dragInfo.Data is string)) { if (!useDefaultDragAdorner && ((IEnumerable)dragInfo.Data).Cast <object>().Count() <= 10) { var itemsControl = new ItemsControl(); itemsControl.ItemsSource = (IEnumerable)dragInfo.Data; itemsControl.ItemTemplate = template; itemsControl.ItemTemplateSelector = templateSelector; itemsControl.Tag = dragInfo; if (useVisualSourceItemSizeForDragAdorner) { var bounds = VisualTreeHelper.GetDescendantBounds(dragInfo.VisualSourceItem); itemsControl.SetValue(FrameworkElement.MinWidthProperty, bounds.Width); } // The ItemsControl doesn't display unless we create a grid to contain it. // Not quite sure why we need this... var grid = new Grid(); grid.Children.Add(itemsControl); adornment = grid; } } else { var contentPresenter = new ContentPresenter(); contentPresenter.Content = dragInfo.Data; contentPresenter.ContentTemplate = template; contentPresenter.ContentTemplateSelector = templateSelector; contentPresenter.Tag = dragInfo; if (useVisualSourceItemSizeForDragAdorner) { var bounds = VisualTreeHelper.GetDescendantBounds(dragInfo.VisualSourceItem); contentPresenter.SetValue(FrameworkElement.MinWidthProperty, bounds.Width); contentPresenter.SetValue(FrameworkElement.MinHeightProperty, bounds.Height); } adornment = contentPresenter; } } if (adornment != null) { if (useDefaultDragAdorner) { adornment.Opacity = GetDefaultDragAdornerOpacity(dragInfo.VisualSource); } var rootElement = RootElementFinder.FindRoot(dropInfo.VisualTarget ?? dragInfo.VisualSource); DragAdorner = new DragAdorner(rootElement, adornment, GetDragAdornerTranslation(dragInfo.VisualSource)); } }