private static void CreateDragAdorner() { DataTemplate template = GetDragAdornerTemplate(_dragInfo.VisualSource); if (template == null) return; var parentWindow = _dragInfo.VisualSource.GetVisualAncestor<Window>(); UIElement rootElement = null; UIElement adornment = null; if (parentWindow != null) rootElement = parentWindow.Content as UIElement; if (rootElement == null) rootElement = (UIElement)Application.Current.MainWindow.Content; if (_dragInfo.Data is IEnumerable && !(_dragInfo.Data is string)) { if (((IEnumerable)_dragInfo.Data).Cast<object>().Count() <= 10) { var itemsControl = new ItemsControl(); itemsControl.ItemsSource = (IEnumerable)_dragInfo.Data; itemsControl.ItemTemplate = template; var border = new Border(); border.Child = itemsControl; adornment = border; } } else { var contentPresenter = new ContentPresenter(); contentPresenter.Content = _dragInfo.Data; contentPresenter.ContentTemplate = template; adornment = contentPresenter; } if (adornment == null) return; adornment.Opacity = 0.5; DragAdorner = new DragAdorner(rootElement, adornment); }
private static void DropTarget_PreviewDragOver(object sender, DragEventArgs e) { var itemsControl = sender as ItemsControl; var dropInfo = new DropInfo(sender, e, _dragInfo); IDropTarget dropHandler = GetDropHandler((UIElement)sender); if (dropHandler != null) { dropHandler.DragOver(dropInfo); } else { DefaultDropHandler.DragOver(dropInfo); } // Update the drag adorner. if (dropInfo.Effects != DragDropEffects.None) { if (DragAdorner == null && _dragInfo != null) { CreateDragAdorner(); } if (DragAdorner != null) { DragAdorner.MousePosition = e.GetPosition(DragAdorner.AdornedElement); DragAdorner.InvalidateVisual(); } } else { DragAdorner = null; } // 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 = (UIElement)itemsControl.GetVisualDescendant <ItemsPresenter>() ?? (UIElement)itemsControl.GetVisualDescendant <ScrollContentPresenter>(); if (adornedElement != null) { if (dropInfo.DropTargetAdorner == null) { DropTargetAdorner = null; } else if (!dropInfo.DropTargetAdorner.IsInstanceOfType(DropTargetAdorner)) { DropTargetAdorner = DropTargetAdorner.Create(dropInfo.DropTargetAdorner, adornedElement); } if (DropTargetAdorner != null) { DropTargetAdorner.DropInfo = dropInfo; DropTargetAdorner.InvalidateVisual(); } } } e.Effects = dropInfo.Effects; e.Handled = true; Scroll((DependencyObject)sender, e); }