Skip to content

Commit 6ba960f

Browse files
committed
Fix not working DragDropCopyKeyState property
Closes #200 Shortcut for copy-paste / cut-paste functionality? Closes #211 ListBox DragDropKeyStates example moves when being told to copy Closes #223
1 parent deb1ec1 commit 6ba960f

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

src/GongSolutions.WPF.DragDrop.Shared/DefaultDropHandler.cs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Windows;
88
using GongSolutions.Wpf.DragDrop.Utilities;
99
using System.Windows.Controls;
10-
using System.Windows.Input;
1110

1211
namespace GongSolutions.Wpf.DragDrop
1312
{
@@ -16,6 +15,27 @@ namespace GongSolutions.Wpf.DragDrop
1615
/// </summary>
1716
public class DefaultDropHandler : IDropTarget
1817
{
18+
/// <summary>
19+
/// Determines whether the data of the drag drop action should be copied otherwise moved.
20+
/// </summary>
21+
/// <param name="dropInfo">The DropInfo with a valid DragInfo.</param>
22+
public static bool ShouldCopyData(IDropInfo dropInfo)
23+
{
24+
// default should always the move action/effect
25+
if (dropInfo == null || dropInfo.DragInfo == null)
26+
{
27+
return false;
28+
}
29+
var copyData = ((dropInfo.DragInfo.DragDropCopyKeyState != default(DragDropKeyStates)) && dropInfo.KeyStates.HasFlag(dropInfo.DragInfo.DragDropCopyKeyState))
30+
|| dropInfo.DragInfo.DragDropCopyKeyState.HasFlag(DragDropKeyStates.LeftMouseButton);
31+
copyData = copyData
32+
//&& (dropInfo.DragInfo.VisualSource != dropInfo.VisualTarget)
33+
&& !(dropInfo.DragInfo.SourceItem is HeaderedContentControl)
34+
&& !(dropInfo.DragInfo.SourceItem is HeaderedItemsControl)
35+
&& !(dropInfo.DragInfo.SourceItem is ListBoxItem);
36+
return copyData;
37+
}
38+
1939
/// <summary>
2040
/// Updates the current drag state.
2141
/// </summary>
@@ -28,13 +48,7 @@ public class DefaultDropHandler : IDropTarget
2848
public virtual void DragOver(IDropInfo dropInfo)
2949
{
3050
if (CanAcceptData(dropInfo)) {
31-
// default should always the move action/effect
32-
var copyData = (dropInfo.DragInfo.DragDropCopyKeyState != default(DragDropKeyStates)) && dropInfo.KeyStates.HasFlag(dropInfo.DragInfo.DragDropCopyKeyState)
33-
//&& (dropInfo.DragInfo.VisualSource != dropInfo.VisualTarget)
34-
&& !(dropInfo.DragInfo.SourceItem is HeaderedContentControl)
35-
&& !(dropInfo.DragInfo.SourceItem is HeaderedItemsControl)
36-
&& !(dropInfo.DragInfo.SourceItem is ListBoxItem);
37-
dropInfo.Effects = copyData ? DragDropEffects.Copy : DragDropEffects.Move;
51+
dropInfo.Effects = ShouldCopyData(dropInfo) ? DragDropEffects.Copy : DragDropEffects.Move;
3852
var isTreeViewItem = dropInfo.InsertPosition.HasFlag(RelativeInsertPosition.TargetItemCenter) && dropInfo.VisualTargetItem is TreeViewItem;
3953
dropInfo.DropTargetAdorner = isTreeViewItem ? DropTargetAdorners.Highlight : DropTargetAdorners.Insert;
4054
}
@@ -73,12 +87,7 @@ public virtual void Drop(IDropInfo dropInfo)
7387
var destinationList = dropInfo.TargetCollection.TryGetList();
7488
var data = ExtractData(dropInfo.Data);
7589

76-
// default should always the move action/effect
77-
var copyData = (dropInfo.DragInfo.DragDropCopyKeyState != default(DragDropKeyStates)) && dropInfo.KeyStates.HasFlag(dropInfo.DragInfo.DragDropCopyKeyState)
78-
//&& (dropInfo.DragInfo.VisualSource != dropInfo.VisualTarget)
79-
&& !(dropInfo.DragInfo.SourceItem is HeaderedContentControl)
80-
&& !(dropInfo.DragInfo.SourceItem is HeaderedItemsControl)
81-
&& !(dropInfo.DragInfo.SourceItem is ListBoxItem);
90+
var copyData = ShouldCopyData(dropInfo);
8291
if (!copyData)
8392
{
8493
var sourceList = dropInfo.DragInfo.SourceCollection.TryGetList();

src/GongSolutions.WPF.DragDrop.Shared/DropInfo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public DropInfo(object sender, DragEventArgs e, DragInfo dragInfo)
4444
this.Data = (e.Data.GetDataPresent(dataFormat)) ? e.Data.GetData(dataFormat) : e.Data;
4545
this.DragInfo = dragInfo;
4646
this.KeyStates = e.KeyStates;
47+
this.Effects = dragInfo.Effects;
4748

4849
this.VisualTarget = sender as UIElement;
4950
// if there is no drop target, find another

0 commit comments

Comments
 (0)