Skip to content

Commit

Permalink
Custom drop adorner sample for Issue #85
Browse files Browse the repository at this point in the history
  • Loading branch information
punker76 committed Aug 29, 2016
1 parent 2ed1258 commit 939d796
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<object>().ToArray());
((TextBox) dropInfo.VisualTarget).Text = string.Join(", ", dataAsList.OfType<object>().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);
}
}
}
}
10 changes: 6 additions & 4 deletions src/Showcase.WPF.DragDrop.NET45/Views/Issues.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -496,10 +496,12 @@
ItemsSource="{Binding Data.Collection1}"
dd:DragDrop.IsDragSource="True"
dd:DragDrop.IsDropTarget="True" />
<Grid Grid.Column="1">
<TextBox Text="Drop any item here..."
IsReadOnly="True"
Height="30"
<Grid Grid.Column="1"
Margin="4">
<TextBox Text="Drop any item on this TextBox..."
Height="150"
TextWrapping="Wrap"
VerticalScrollBarVisibility="Auto"
dd:DragDrop.IsDropTarget="True"
dd:DragDrop.DropHandler="{Binding Data.TextBoxCustomDropHandler}" />
</Grid>
Expand Down

0 comments on commit 939d796

Please sign in to comment.