private static void CreateDragAdorner() { var template = GetDragAdornerTemplate(_dragInfo.VisualSource); var templateSelector = GetDragAdornerTemplateSelector(_dragInfo.VisualSource); UIElement adornment = null; var useDefaultDragAdorner = GetUseDefaultDragAdorner(_dragInfo.VisualSource); if (template == null && templateSelector == null && useDefaultDragAdorner) { template = new DataTemplate(); var factory = new FrameworkElementFactory(typeof(Image)); var bs = _dragInfo.VisualSourceItem.RenderToBitmap(_dragInfo.VisualSourceFlowDirection); factory.SetValue(Image.SourceProperty, bs); factory.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased); factory.SetValue(RenderOptions.BitmapScalingModeProperty, BitmapScalingMode.HighQuality); if (_dragInfo.VisualSourceItem is FrameworkElement) { factory.SetValue(FrameworkElement.WidthProperty, ((FrameworkElement)_dragInfo.VisualSourceItem).ActualWidth); factory.SetValue(FrameworkElement.HeightProperty, ((FrameworkElement)_dragInfo.VisualSourceItem).ActualHeight); factory.SetValue(FrameworkElement.HorizontalAlignmentProperty, HorizontalAlignment.Left); factory.SetValue(FrameworkElement.VerticalAlignmentProperty, VerticalAlignment.Top); } template.VisualTree = factory; } if (template != null || templateSelector != null) { 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; itemsControl.ItemTemplateSelector = templateSelector; // The ItemsControl doesn't display unless we create a border to contain it. // Not quite sure why this is... var border = new Border(); border.Child = itemsControl; adornment = border; } } else { var contentPresenter = new ContentPresenter(); contentPresenter.Content = _dragInfo.Data; contentPresenter.ContentTemplate = template; contentPresenter.ContentTemplateSelector = templateSelector; adornment = contentPresenter; } } if (adornment != null) { if (useDefaultDragAdorner) { adornment.Opacity = GetDefaultDragAdornerOpacity(_dragInfo.VisualSource); } var parentWindow = _dragInfo.VisualSource.FindVisualParent<Window>(); var rootElement = parentWindow != null ? parentWindow.Content as UIElement : null; if (rootElement == null && Application.Current != null && Application.Current.MainWindow != null) { rootElement = (UIElement)Application.Current.MainWindow.Content; } if (rootElement == null) { rootElement = _dragInfo.VisualSource.FindVisualParent<UserControl>(); } DragAdorner = new DragAdorner(rootElement, adornment); } }
private static void CreateEffectAdorner(DropInfo dropInfo) { var template = GetEffectAdornerTemplate(dropInfo.VisualTarget, dropInfo.AllowedEffects, dropInfo.DestinationText); if (template != null) { var rootElement = (UIElement)Application.Current.MainWindow.Content; UIElement adornment = null; var contentPresenter = new ContentPresenter(); contentPresenter.Content = _dragInfo.Data; contentPresenter.ContentTemplate = template; adornment = contentPresenter; EffectAdorner = new DragAdorner(rootElement, adornment); } }