From 939d79607a34dc6e64fe2082046df4a144abbebb Mon Sep 17 00:00:00 2001 From: Jan Karger Date: Mon, 29 Aug 2016 22:40:59 +0200 Subject: [PATCH] Custom drop adorner sample for Issue #85 --- .../Models/TextBoxCustomDropHandler.cs | 34 ++++++++++++++++++- .../Views/Issues.xaml | 10 +++--- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/Showcase.WPF.DragDrop.NET45/Models/TextBoxCustomDropHandler.cs b/src/Showcase.WPF.DragDrop.NET45/Models/TextBoxCustomDropHandler.cs index f9f25bf0..adc68ea5 100644 --- a/src/Showcase.WPF.DragDrop.NET45/Models/TextBoxCustomDropHandler.cs +++ b/src/Showcase.WPF.DragDrop.NET45/Models/TextBoxCustomDropHandler.cs @@ -1,6 +1,7 @@ using System.Linq; using System.Windows; using System.Windows.Controls; +using System.Windows.Media; using GongSolutions.Wpf.DragDrop; namespace Showcase.WPF.DragDrop.Models @@ -9,13 +10,44 @@ public class TextBoxCustomDropHandler : IDropTarget { public void DragOver(IDropInfo dropInfo) { + dropInfo.DropTargetAdorner = typeof(DropTargetHighlightAdorner); dropInfo.Effects = DragDropEffects.Move; } public void Drop(IDropInfo dropInfo) { var dataAsList = DefaultDropHandler.ExtractData(dropInfo.Data); - ((TextBox)dropInfo.VisualTarget).Text = string.Join(", ", dataAsList.OfType().ToArray()); + ((TextBox) dropInfo.VisualTarget).Text = string.Join(", ", dataAsList.OfType().ToArray()); + } + } + + public class DropTargetHighlightAdorner : DropTargetAdorner + { + private readonly Pen _pen; + private readonly Brush _brush; + + public DropTargetHighlightAdorner(UIElement adornedElement) + : base(adornedElement) + { + _pen = new Pen(Brushes.Tomato, 2); + _pen.Freeze(); + _brush = new SolidColorBrush(Colors.Coral) {Opacity = 0.2}; + this._brush.Freeze(); + + this.SetValue(SnapsToDevicePixelsProperty, true); + } + + protected override void OnRender(DrawingContext drawingContext) + { + var visualTarget = this.DropInfo.VisualTarget; + if (visualTarget != null) + { + var translatePoint = visualTarget.TranslatePoint(new Point(), this.AdornedElement); + translatePoint.Offset(1, 1); + var bounds = new Rect(translatePoint, + new Size(visualTarget.RenderSize.Width - 2, visualTarget.RenderSize.Height - 2)); + drawingContext.DrawRectangle(_brush, _pen, bounds); + } } } } \ No newline at end of file diff --git a/src/Showcase.WPF.DragDrop.NET45/Views/Issues.xaml b/src/Showcase.WPF.DragDrop.NET45/Views/Issues.xaml index ef7e2f30..74392b68 100644 --- a/src/Showcase.WPF.DragDrop.NET45/Views/Issues.xaml +++ b/src/Showcase.WPF.DragDrop.NET45/Views/Issues.xaml @@ -496,10 +496,12 @@ ItemsSource="{Binding Data.Collection1}" dd:DragDrop.IsDragSource="True" dd:DragDrop.IsDropTarget="True" /> - - +