Skip to content

Commit

Permalink
Merge pull request #86 from punker76/85-drop-to-textbox
Browse files Browse the repository at this point in the history
fix issue #85
  • Loading branch information
punker76 committed Feb 12, 2014
2 parents 2a6c014 + 9f3dec4 commit 7c892ae
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 23 deletions.
19 changes: 19 additions & 0 deletions Examples/DefaultsExample/Data.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
Expand All @@ -13,6 +14,20 @@

namespace DefaultsExample
{
public class CustomDropHandlerForIssue85 : IDropTarget
{
public void DragOver(IDropInfo dropInfo)
{
dropInfo.Effects = DragDropEffects.Copy;
}

public void Drop(IDropInfo dropInfo)
{
MessageBox.Show("He, now it works :-D");
((TextBox)dropInfo.VisualTarget).Text = (string)dropInfo.Data;
}
}

internal class Data : IDropTarget
{
public Data()
Expand All @@ -23,6 +38,8 @@ public Data()
this.CustomCollection1 = new ObservableCollection<CustomDataModel>();
this.CustomCollection2 = new ObservableCollection<CustomDataModel>();

this.CustomDropHandler = new CustomDropHandlerForIssue85();

for (var n = 0; n < 100; ++n) {
this.Collection1.Add("Item " + n);
this.CustomCollection1.Add(new CustomDataModel { Name = "Custom Item " + n });
Expand Down Expand Up @@ -58,6 +75,8 @@ public Data()
public ObservableCollection<GroupedItem> GroupedCollection { get; private set; }
public ObservableCollection<TreeNode> TreeCollection { get; private set; }

public CustomDropHandlerForIssue85 CustomDropHandler { get; private set; }

//
// The drop handler is only used for the grouping example.
//
Expand Down
21 changes: 20 additions & 1 deletion Examples/DefaultsExample/Window(NET4).xaml
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@
ItemTemplate="{StaticResource CustomTemplate}"
dd:DragDrop.IsDragSource="True"
dd:DragDrop.IsDropTarget="True"
dd:DragDrop.DragAdornerTemplate="{StaticResource DragAdorner}" />
dd:DragDrop.DragAdornerTemplate="{StaticResource CustomTemplate}" />
</Grid>
</TabItem>

Expand Down Expand Up @@ -855,5 +855,24 @@
ItemsSource="{Binding Collection3}" />
</Grid>
</TabItem>

<TabItem Header="Drop to TextBox, issue #85">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.ColumnSpan="2">This example uses a DataTemplateSelector for the drag adorner.</TextBlock>
<ListBox ItemsSource="{Binding Collection1}"
Grid.Column="0"
dd:DragDrop.IsDragSource="True"></ListBox>
<TextBox Text="Drop the item here"
IsReadOnly="True"
Grid.Column="1"
Height="30"
dd:DragDrop.IsDropTarget="True"
dd:DragDrop.DropHandler="{Binding CustomDropHandler}" />
</Grid>
</TabItem>
</TabControl>
</Window>
50 changes: 30 additions & 20 deletions GongSolutions.Wpf.DragDrop/DragDrop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,28 +332,38 @@ private static void IsDropTargetChanged(DependencyObject d, DependencyPropertyCh

if ((bool)e.NewValue == true) {
uiElement.AllowDrop = true;
//uiElement.PreviewDragEnter += DropTarget_PreviewDragEnter;
//uiElement.PreviewDragLeave += DropTarget_PreviewDragLeave;
//uiElement.PreviewDragOver += DropTarget_PreviewDragOver;
//uiElement.PreviewDrop += DropTarget_PreviewDrop;

uiElement.DragEnter += DropTarget_PreviewDragEnter;
uiElement.DragLeave += DropTarget_PreviewDragLeave;
uiElement.DragOver += DropTarget_PreviewDragOver;
uiElement.Drop += DropTarget_PreviewDrop;
uiElement.GiveFeedback += DropTarget_GiveFeedback;

if (uiElement is ItemsControl) {
// use normal events for ItemsControls
uiElement.DragEnter += DropTarget_PreviewDragEnter;
uiElement.DragLeave += DropTarget_PreviewDragLeave;
uiElement.DragOver += DropTarget_PreviewDragOver;
uiElement.Drop += DropTarget_PreviewDrop;
uiElement.GiveFeedback += DropTarget_GiveFeedback;
} else {
// issue #85: try using preview events for all other elements than ItemsControls
uiElement.PreviewDragEnter += DropTarget_PreviewDragEnter;
uiElement.PreviewDragLeave += DropTarget_PreviewDragLeave;
uiElement.PreviewDragOver += DropTarget_PreviewDragOver;
uiElement.PreviewDrop += DropTarget_PreviewDrop;
uiElement.PreviewGiveFeedback += DropTarget_GiveFeedback;
}
} else {
uiElement.AllowDrop = false;
//uiElement.PreviewDragEnter -= DropTarget_PreviewDragEnter;
//uiElement.PreviewDragLeave -= DropTarget_PreviewDragLeave;
//uiElement.PreviewDragOver -= DropTarget_PreviewDragOver;
//uiElement.PreviewDrop -= DropTarget_PreviewDropTest;

uiElement.DragEnter -= DropTarget_PreviewDragEnter;
uiElement.DragLeave -= DropTarget_PreviewDragLeave;
uiElement.DragOver -= DropTarget_PreviewDragOver;
uiElement.Drop -= DropTarget_PreviewDrop;
uiElement.GiveFeedback -= DropTarget_GiveFeedback;

if (uiElement is ItemsControl) {
uiElement.DragEnter -= DropTarget_PreviewDragEnter;
uiElement.DragLeave -= DropTarget_PreviewDragLeave;
uiElement.DragOver -= DropTarget_PreviewDragOver;
uiElement.Drop -= DropTarget_PreviewDrop;
uiElement.GiveFeedback -= DropTarget_GiveFeedback;
} else {
uiElement.PreviewDragEnter -= DropTarget_PreviewDragEnter;
uiElement.PreviewDragLeave -= DropTarget_PreviewDragLeave;
uiElement.PreviewDragOver -= DropTarget_PreviewDragOver;
uiElement.PreviewDrop -= DropTarget_PreviewDrop;
uiElement.PreviewGiveFeedback -= DropTarget_GiveFeedback;
}

Mouse.OverrideCursor = null;
}
Expand Down
12 changes: 10 additions & 2 deletions GongSolutions.Wpf.DragDrop/DropInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,17 @@ public DropInfo(object sender, DragEventArgs e, DragInfo dragInfo)
this.VisualTarget = sender as UIElement;
// if drop target isn't a ItemsControl
if (!(this.VisualTarget is ItemsControl)) {
this.VisualTarget = VisualTreeExtensions.GetVisualAncestor<ItemsControl>(this.VisualTarget) ?? this.VisualTarget;
// try to find next ItemsControl
var itemsControl = VisualTreeExtensions.GetVisualAncestor<ItemsControl>(this.VisualTarget);
if (itemsControl != null) {
// now check if this ItemsControl is a drop target
if (DragDrop.GetIsDropTarget(itemsControl)) {
this.VisualTarget = itemsControl;
}
}
}
this.DropPosition = e.GetPosition(this.VisualTarget);
// visual target can be null, so give us a point...
this.DropPosition = this.VisualTarget != null ? e.GetPosition(this.VisualTarget) : new Point();

if (this.VisualTarget is ItemsControl) {
var itemsControl = (ItemsControl)this.VisualTarget;
Expand Down

0 comments on commit 7c892ae

Please sign in to comment.