-
Notifications
You must be signed in to change notification settings - Fork 0
/
DragAndDropBehavior.cs
286 lines (235 loc) · 10.8 KB
/
DragAndDropBehavior.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
namespace Behaviors
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Collections;
using System.Windows.Media.Imaging;
using System.Drawing;
using System.IO;
using System.Windows.Threading;
using System.Windows.Data;
using System.Runtime.CompilerServices;
using ModuleTwo.Views;
using System.Windows.Controls.Primitives;
public static class DragAndDropBehavior
{
private static Dictionary<String, List<ItemsControl>> dragSources;
private static Dictionary<String, List<ItemsControl>> dropTargets;
// Mouse click coordinates
private static System.Windows.Point startPoint;
public static readonly DependencyProperty IsDropTargetProperty =
DependencyProperty.RegisterAttached("IsDropTarget", typeof(bool),
typeof(DragAndDropBehavior),
new UIPropertyMetadata(false, IsDropTargetUpdated));
public static readonly DependencyProperty IsDragSourceProperty =
DependencyProperty.RegisterAttached("IsDragSource", typeof(bool),
typeof(DragAndDropBehavior),
new UIPropertyMetadata(false, IsDragSourceUpdated));
public static readonly DependencyProperty GroupNameProperty =
DependencyProperty.RegisterAttached("GroupName", typeof(String),
typeof(DragAndDropBehavior),
new UIPropertyMetadata("", GroupNameUpdated));
public static String GetGroupName(DependencyObject obj)
{
return (String)obj.GetValue(GroupNameProperty);
}
public static void SetGroupName(DependencyObject obj, String value)
{
obj.SetValue(GroupNameProperty, value);
}
public static bool GetIsDropTarget(DependencyObject obj)
{
return (bool)obj.GetValue(IsDropTargetProperty);
}
public static void SetIsDropTarget(DependencyObject obj, bool value)
{
obj.SetValue(IsDropTargetProperty, value);
}
public static bool GetIsDragSource(DependencyObject obj)
{
return (bool)obj.GetValue(IsDragSourceProperty);
}
public static void SetIsDragSource(DependencyObject obj, bool value)
{
obj.SetValue(IsDragSourceProperty, value);
}
private static void InitializeDragDropCollections()
{
if (dragSources == null)
dragSources = new Dictionary<string, List<ItemsControl>>();
if (dropTargets == null)
dropTargets = new Dictionary<string, List<ItemsControl>>();
if(!dragSources.Any(p => p.Key == ""))
dragSources.Add("", new List<ItemsControl>());
if (!dropTargets.Any(p => p.Key == ""))
dropTargets.Add("", new List<ItemsControl>());
}
// Adds passed ItemsControl to the drag sources collection
private static void IsDragSourceUpdated(DependencyObject dp,
DependencyPropertyChangedEventArgs args)
{
var isDragSourceEnabled = (bool)args.NewValue;
var dragSource = dp as ItemsControl;
if (isDragSourceEnabled)
{
Add(dragSources, dragSource);
dragSource.PreviewMouseMove += OnMouseMove;
dragSource.PreviewMouseLeftButtonDown += OnLeftButtonDown;
}
else
{
Remove(dragSources, dragSource);
dragSource.PreviewMouseMove -= OnMouseMove;
dragSource.PreviewMouseLeftButtonDown -= OnLeftButtonDown;
}
}
// Adds passed ItemsControl to the drop targets collection
private static void IsDropTargetUpdated(DependencyObject dp,
DependencyPropertyChangedEventArgs args)
{
var isDropTargetEnabled = (bool)args.NewValue;
var dropTarget = dp as ItemsControl;
dropTarget.AllowDrop = isDropTargetEnabled;
if (isDropTargetEnabled)
{
Add(dropTargets, dropTarget);
dropTarget.Drop += Drop;
}
else
{
Remove(dropTargets, dropTarget);
dropTarget.Drop -= Drop;
}
}
// Adds item control to a group
// (this item control will only be allowed to participate in d&d in this particular gorup)
private static void Add(Dictionary<String, List<ItemsControl>> dictionary, object sender)
{
InitializeDragDropCollections();
var dp = sender as DependencyObject;
var itemsControl = sender as ItemsControl;
var groupName = GetGroupName(dp);
var foundGroup = dictionary.FirstOrDefault(p => p.Key == groupName);
if (!foundGroup.Value.Contains(itemsControl))
dictionary[groupName].Add(itemsControl);
itemsControl.Unloaded += DropTarget_Unloaded;
itemsControl.Loaded += DropTarget_Loaded;
}
// Removes item control from group
private static void Remove(Dictionary<String, List<ItemsControl>> dictionary, object sender)
{
var dp = sender as DependencyObject;
var itemsControl = sender as ItemsControl;
var groupName = GetGroupName(dp);
var foundGroup = dictionary.FirstOrDefault(p => p.Key == groupName);
if (foundGroup.Value.Contains(itemsControl))
dictionary[groupName].Remove(itemsControl);
itemsControl.Unloaded -= DropTarget_Unloaded;
itemsControl.Loaded -= DropTarget_Loaded;
}
private static void GroupNameUpdated(DependencyObject dp, DependencyPropertyChangedEventArgs args)
{
var itemsControl = dp as ItemsControl;
string newGroupName = (string)args.NewValue;
InitializeDragDropCollections();
if (!dragSources.Any(p => p.Key == newGroupName))
dragSources.Add((String)args.NewValue, new List<ItemsControl>());
if (!dropTargets.Any(p => p.Key == newGroupName))
dropTargets.Add((String)args.NewValue, new List<ItemsControl>());
var foundCollection = dragSources.Where(p => p.Value.Any(k => k == itemsControl) == true);
if (foundCollection != null && foundCollection.Count() > 0)
{
foundCollection.First().Value.Remove(itemsControl);
if (!dragSources[((String)args.NewValue)].Any(p => p == itemsControl))
dragSources[((String)args.NewValue)].Add(itemsControl);
}
}
private static void Drop(object sender, DragEventArgs e)
{
ItemsControl parent = (ItemsControl)sender;
// Checking if there's a group that has control assigned to it
if (!dragSources[GetGroupName(parent as DependencyObject)].Any(p => p == parent))
return;
// Get the type of data that's used for the object transfered between containers
var dataType = parent.ItemsSource.GetType().GetGenericArguments().Single();
// Aquiring data of the particular type
var data = e.Data.GetData(dataType);
// This will hit when we'll try to drop garbage into the container
if (data == null)
return;
// We don't wanna drop the same data to the same control again
if (((IList)parent.ItemsSource).Contains(data))
return;
var foundControl = dragSources[GetGroupName(parent as DependencyObject)]
.Find(p => ((IList)p.ItemsSource).Contains(data));
if (foundControl == null)
return;
((IList)foundControl.ItemsSource).Remove(data);
((IList)parent.ItemsSource).Add(data);
BindingOperations.GetBindingExpressionBase(parent as DependencyObject,
ItemsControl.ItemsSourceProperty).UpdateTarget();
BindingOperations.GetBindingExpressionBase(foundControl as DependencyObject,
ItemsControl.ItemsSourceProperty).UpdateTarget();
}
private static void OnMouseMove(object sender, MouseEventArgs e)
{
ItemsControl source = (ItemsControl)sender;
System.Windows.Point currentPos = e.GetPosition(null);
Vector diff = startPoint - currentPos;
if (e.LeftButton == MouseButtonState.Pressed &&
( Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
{
object data = GetDataFromSource(source, e.GetPosition(source));
if (data != null)
{
DragDrop.DoDragDrop(source as DependencyObject, data, DragDropEffects.Move);
}
}
}
private static object GetDataFromSource(ItemsControl source, System.Windows.Point point)
{
UIElement element = source.InputHitTest(point) as UIElement;
if (element != null)
{
object data = DependencyProperty.UnsetValue;
while (data == DependencyProperty.UnsetValue)
{
element = VisualTreeHelper.GetParent(element) as UIElement;
if (element == source)
return null;
data = source.ItemContainerGenerator.ItemFromContainer(element);
}
if (data != DependencyProperty.UnsetValue)
return data;
}
return null;
}
private static void OnLeftButtonDown(object sender, MouseButtonEventArgs e)
{
startPoint = e.GetPosition(null);
}
private static void DragSource_Loaded(object sender, RoutedEventArgs e)
{
Add(dragSources, sender);
}
private static void DragSource_Unloaded(object sender, RoutedEventArgs e)
{
Remove(dragSources, sender);
}
private static void DropTarget_Loaded(object sender, RoutedEventArgs e)
{
Add(dropTargets, sender);
}
private static void DropTarget_Unloaded(object sender, RoutedEventArgs e)
{
Remove(dropTargets, sender);
}
}
}