-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListViewDragDropManager.cs
864 lines (686 loc) · 23.7 KB
/
ListViewDragDropManager.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
// Copyright (C) Josh Smith - January 2007
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Input;
using WPF.JoshSmith.Adorners;
using WPF.JoshSmith.Controls.Utilities;
namespace WPF.JoshSmith.ServiceProviders.UI
{
#region ListViewDragDropManager
/// <summary>
/// Manages the dragging and dropping of ListViewItems in a ListView.
/// The ItemType type parameter indicates the type of the objects in
/// the ListView's items source. The ListView's ItemsSource must be
/// set to an instance of ObservableCollection of ItemType, or an
/// Exception will be thrown.
/// </summary>
/// <typeparam name="ItemType">The type of the ListView's items.</typeparam>
public class ListViewDragDropManager<ItemType> where ItemType : class
{
#region Data
bool canInitiateDrag;
DragAdorner dragAdorner;
double dragAdornerOpacity;
int indexToSelect;
bool isDragInProgress;
ItemType itemUnderDragCursor;
ListView listView;
Point ptMouseDown;
bool showDragAdorner;
#endregion // Data
#region Constructors
/// <summary>
/// Initializes a new instance of ListViewDragManager.
/// </summary>
public ListViewDragDropManager()
{
this.canInitiateDrag = false;
this.dragAdornerOpacity = 0.7;
this.indexToSelect = -1;
this.showDragAdorner = true;
}
/// <summary>
/// Initializes a new instance of ListViewDragManager.
/// </summary>
/// <param name="listView"></param>
public ListViewDragDropManager( ListView listView )
: this()
{
this.ListView = listView;
}
/// <summary>
/// Initializes a new instance of ListViewDragManager.
/// </summary>
/// <param name="listView"></param>
/// <param name="dragAdornerOpacity"></param>
public ListViewDragDropManager( ListView listView, double dragAdornerOpacity )
: this( listView )
{
this.DragAdornerOpacity = dragAdornerOpacity;
}
/// <summary>
/// Initializes a new instance of ListViewDragManager.
/// </summary>
/// <param name="listView"></param>
/// <param name="showDragAdorner"></param>
public ListViewDragDropManager( ListView listView, bool showDragAdorner )
: this( listView )
{
this.ShowDragAdorner = showDragAdorner;
}
#endregion // Constructors
#region Public Interface
#region DragAdornerOpacity
/// <summary>
/// Gets/sets the opacity of the drag adorner. This property has no
/// effect if ShowDragAdorner is false. The default value is 0.7
/// </summary>
public double DragAdornerOpacity
{
get { return this.dragAdornerOpacity; }
set
{
if( this.IsDragInProgress )
throw new InvalidOperationException( "Cannot set the DragAdornerOpacity property during a drag operation." );
if( value < 0.0 || value > 1.0 )
throw new ArgumentOutOfRangeException( "DragAdornerOpacity", value, "Must be between 0 and 1." );
this.dragAdornerOpacity = value;
}
}
#endregion // DragAdornerOpacity
#region IsDragInProgress
/// <summary>
/// Returns true if there is currently a drag operation being managed.
/// </summary>
public bool IsDragInProgress
{
get { return this.isDragInProgress; }
private set { this.isDragInProgress = value; }
}
#endregion // IsDragInProgress
#region ListView
/// <summary>
/// Gets/sets the ListView whose dragging is managed. This property
/// can be set to null, to prevent drag management from occuring. If
/// the ListView's AllowDrop property is false, it will be set to true.
/// </summary>
public ListView ListView
{
get { return listView; }
set
{
if( this.IsDragInProgress )
throw new InvalidOperationException( "Cannot set the ListView property during a drag operation." );
if( this.listView != null )
{
#region Unhook Events
this.listView.PreviewMouseLeftButtonDown -= listView_PreviewMouseLeftButtonDown;
this.listView.PreviewMouseMove -= listView_PreviewMouseMove;
this.listView.DragOver -= listView_DragOver;
this.listView.DragLeave -= listView_DragLeave;
this.listView.DragEnter -= listView_DragEnter;
this.listView.Drop -= listView_Drop;
#endregion // Unhook Events
}
this.listView = value;
if( this.listView != null )
{
if( !this.listView.AllowDrop )
this.listView.AllowDrop = true;
#region Hook Events
this.listView.PreviewMouseLeftButtonDown += listView_PreviewMouseLeftButtonDown;
this.listView.PreviewMouseMove += listView_PreviewMouseMove;
this.listView.DragOver += listView_DragOver;
this.listView.DragLeave += listView_DragLeave;
this.listView.DragEnter += listView_DragEnter;
this.listView.Drop += listView_Drop;
#endregion // Hook Events
}
}
}
#endregion // ListView
#region ProcessDrop [event]
/// <summary>
/// Raised when a drop occurs. By default the dropped item will be moved
/// to the target index. Handle this event if relocating the dropped item
/// requires custom behavior. Note, if this event is handled the default
/// item dropping logic will not occur.
/// </summary>
public event EventHandler<ProcessDropEventArgs<ItemType>> ProcessDrop;
#endregion // ProcessDrop [event]
#region ShowDragAdorner
/// <summary>
/// Gets/sets whether a visual representation of the ListViewItem being dragged
/// follows the mouse cursor during a drag operation. The default value is true.
/// </summary>
public bool ShowDragAdorner
{
get { return this.showDragAdorner; }
set
{
if( this.IsDragInProgress )
throw new InvalidOperationException( "Cannot set the ShowDragAdorner property during a drag operation." );
this.showDragAdorner = value;
}
}
#endregion // ShowDragAdorner
#endregion // Public Interface
#region Event Handling Methods
#region listView_PreviewMouseLeftButtonDown
void listView_PreviewMouseLeftButtonDown( object sender, MouseButtonEventArgs e )
{
if( this.IsMouseOverScrollbar )
{
// 4/13/2007 - Set the flag to false when cursor is over scrollbar.
this.canInitiateDrag = false;
return;
}
int index = this.IndexUnderDragCursor;
this.canInitiateDrag = index > -1;
if( this.canInitiateDrag )
{
// Remember the location and index of the ListViewItem the user clicked on for later.
this.ptMouseDown = MouseUtilities.GetMousePosition( this.listView );
this.indexToSelect = index;
}
else
{
this.ptMouseDown = new Point( -10000, -10000 );
this.indexToSelect = -1;
}
}
#endregion // listView_PreviewMouseLeftButtonDown
#region listView_PreviewMouseMove
void listView_PreviewMouseMove( object sender, MouseEventArgs e )
{
if( !this.CanStartDragOperation )
return;
// Select the item the user clicked on.
if( this.listView.SelectedIndex != this.indexToSelect )
this.listView.SelectedIndex = this.indexToSelect;
// If the item at the selected index is null, there's nothing
// we can do, so just return;
if( this.listView.SelectedItem == null )
return;
ListViewItem itemToDrag = this.GetListViewItem( this.listView.SelectedIndex );
if( itemToDrag == null )
return;
AdornerLayer adornerLayer = this.ShowDragAdornerResolved ? this.InitializeAdornerLayer( itemToDrag ) : null;
this.InitializeDragOperation( itemToDrag );
this.PerformDragOperation();
this.FinishDragOperation( itemToDrag, adornerLayer );
}
#endregion // listView_PreviewMouseMove
#region listView_DragOver
void listView_DragOver( object sender, DragEventArgs e )
{
e.Effects = DragDropEffects.Move;
if( this.ShowDragAdornerResolved )
this.UpdateDragAdornerLocation();
// Update the item which is known to be currently under the drag cursor.
int index = this.IndexUnderDragCursor;
this.ItemUnderDragCursor = index < 0 ? null : this.ListView.Items[index] as ItemType;
}
#endregion // listView_DragOver
#region listView_DragLeave
void listView_DragLeave( object sender, DragEventArgs e )
{
if( !this.IsMouseOver( this.listView ) )
{
if( this.ItemUnderDragCursor != null )
this.ItemUnderDragCursor = null;
if( this.dragAdorner != null )
this.dragAdorner.Visibility = Visibility.Collapsed;
}
}
#endregion // listView_DragLeave
#region listView_DragEnter
void listView_DragEnter( object sender, DragEventArgs e )
{
if( this.dragAdorner != null && this.dragAdorner.Visibility != Visibility.Visible )
{
// Update the location of the adorner and then show it.
this.UpdateDragAdornerLocation();
this.dragAdorner.Visibility = Visibility.Visible;
}
}
#endregion // listView_DragEnter
#region listView_Drop
void listView_Drop( object sender, DragEventArgs e )
{
if( this.ItemUnderDragCursor != null )
this.ItemUnderDragCursor = null;
e.Effects = DragDropEffects.None;
if( !e.Data.GetDataPresent( typeof( ItemType ) ) )
return;
// Get the data object which was dropped.
ItemType data = e.Data.GetData( typeof( ItemType ) ) as ItemType;
if( data == null )
return;
// Get the ObservableCollection<ItemType> which contains the dropped data object.
ObservableCollection<ItemType> itemsSource = this.listView.ItemsSource as ObservableCollection<ItemType>;
if( itemsSource == null )
throw new Exception(
"A ListView managed by ListViewDragManager must have its ItemsSource set to an ObservableCollection<ItemType>." );
int oldIndex = itemsSource.IndexOf( data );
int newIndex = this.IndexUnderDragCursor;
if( newIndex < 0 )
{
// The drag started somewhere else, and our ListView is empty
// so make the new item the first in the list.
if( itemsSource.Count == 0 )
newIndex = 0;
// The drag started somewhere else, but our ListView has items
// so make the new item the last in the list.
else if( oldIndex < 0 )
newIndex = itemsSource.Count;
// The user is trying to drop an item from our ListView into
// our ListView, but the mouse is not over an item, so don't
// let them drop it.
else
return;
}
// Dropping an item back onto itself is not considered an actual 'drop'.
if( oldIndex == newIndex )
return;
if( this.ProcessDrop != null )
{
// Let the client code process the drop.
ProcessDropEventArgs<ItemType> args = new ProcessDropEventArgs<ItemType>( itemsSource, data, oldIndex, newIndex, e.AllowedEffects );
this.ProcessDrop( this, args );
e.Effects = args.Effects;
}
else
{
// Move the dragged data object from it's original index to the
// new index (according to where the mouse cursor is). If it was
// not previously in the ListBox, then insert the item.
if( oldIndex > -1 )
itemsSource.Move( oldIndex, newIndex );
else
itemsSource.Insert( newIndex, data );
// Set the Effects property so that the call to DoDragDrop will return 'Move'.
e.Effects = DragDropEffects.Move;
}
}
#endregion // listView_Drop
#endregion // Event Handling Methods
#region Private Helpers
#region CanStartDragOperation
bool CanStartDragOperation
{
get
{
if( Mouse.LeftButton != MouseButtonState.Pressed )
return false;
if( !this.canInitiateDrag )
return false;
if( this.indexToSelect == -1 )
return false;
if( !this.HasCursorLeftDragThreshold )
return false;
return true;
}
}
#endregion // CanStartDragOperation
#region FinishDragOperation
void FinishDragOperation( ListViewItem draggedItem, AdornerLayer adornerLayer )
{
// Let the ListViewItem know that it is not being dragged anymore.
ListViewItemDragState.SetIsBeingDragged( draggedItem, false );
this.IsDragInProgress = false;
if( this.ItemUnderDragCursor != null )
this.ItemUnderDragCursor = null;
// Remove the drag adorner from the adorner layer.
if( adornerLayer != null )
{
adornerLayer.Remove( this.dragAdorner );
this.dragAdorner = null;
}
}
#endregion // FinishDragOperation
#region GetListViewItem
ListViewItem GetListViewItem( int index )
{
if( this.listView.ItemContainerGenerator.Status != GeneratorStatus.ContainersGenerated )
return null;
return this.listView.ItemContainerGenerator.ContainerFromIndex( index ) as ListViewItem;
}
ListViewItem GetListViewItem( ItemType dataItem )
{
if( this.listView.ItemContainerGenerator.Status != GeneratorStatus.ContainersGenerated )
return null;
return this.listView.ItemContainerGenerator.ContainerFromItem( dataItem ) as ListViewItem;
}
#endregion // GetListViewItem
#region HasCursorLeftDragThreshold
bool HasCursorLeftDragThreshold
{
get
{
if( this.indexToSelect < 0 )
return false;
ListViewItem item = this.GetListViewItem( this.indexToSelect );
Rect bounds = VisualTreeHelper.GetDescendantBounds( item );
Point ptInItem = this.listView.TranslatePoint( this.ptMouseDown, item );
// In case the cursor is at the very top or bottom of the ListViewItem
// we want to make the vertical threshold very small so that dragging
// over an adjacent item does not select it.
double topOffset = Math.Abs( ptInItem.Y );
double btmOffset = Math.Abs( bounds.Height - ptInItem.Y );
double vertOffset = Math.Min( topOffset, btmOffset );
double width = SystemParameters.MinimumHorizontalDragDistance * 2;
double height = Math.Min( SystemParameters.MinimumVerticalDragDistance, vertOffset ) * 2;
Size szThreshold = new Size( width, height );
Rect rect = new Rect( this.ptMouseDown, szThreshold );
rect.Offset( szThreshold.Width / -2, szThreshold.Height / -2 );
Point ptInListView = MouseUtilities.GetMousePosition( this.listView );
return !rect.Contains( ptInListView );
}
}
#endregion // HasCursorLeftDragThreshold
#region IndexUnderDragCursor
/// <summary>
/// Returns the index of the ListViewItem underneath the
/// drag cursor, or -1 if the cursor is not over an item.
/// </summary>
int IndexUnderDragCursor
{
get
{
int index = -1;
for( int i = 0; i < this.listView.Items.Count; ++i )
{
ListViewItem item = this.GetListViewItem( i );
if( this.IsMouseOver( item ) )
{
index = i;
break;
}
}
return index;
}
}
#endregion // IndexUnderDragCursor
#region InitializeAdornerLayer
AdornerLayer InitializeAdornerLayer( ListViewItem itemToDrag )
{
// Create a brush which will paint the ListViewItem onto
// a visual in the adorner layer.
VisualBrush brush = new VisualBrush( itemToDrag );
// Create an element which displays the source item while it is dragged.
this.dragAdorner = new DragAdorner( this.listView, itemToDrag.RenderSize, brush );
// Set the drag adorner's opacity.
this.dragAdorner.Opacity = this.DragAdornerOpacity;
AdornerLayer layer = AdornerLayer.GetAdornerLayer( this.listView );
layer.Add( dragAdorner );
// Save the location of the cursor when the left mouse button was pressed.
this.ptMouseDown = MouseUtilities.GetMousePosition( this.listView );
return layer;
}
#endregion // InitializeAdornerLayer
#region InitializeDragOperation
void InitializeDragOperation( ListViewItem itemToDrag )
{
// Set some flags used during the drag operation.
this.IsDragInProgress = true;
this.canInitiateDrag = false;
// Let the ListViewItem know that it is being dragged.
ListViewItemDragState.SetIsBeingDragged( itemToDrag, true );
}
#endregion // InitializeDragOperation
#region IsMouseOver
bool IsMouseOver( Visual target )
{
// We need to use MouseUtilities to figure out the cursor
// coordinates because, during a drag-drop operation, the WPF
// mechanisms for getting the coordinates behave strangely.
Rect bounds = VisualTreeHelper.GetDescendantBounds( target );
Point mousePos = MouseUtilities.GetMousePosition( target );
return bounds.Contains( mousePos );
}
#endregion // IsMouseOver
#region IsMouseOverScrollbar
/// <summary>
/// Returns true if the mouse cursor is over a scrollbar in the ListView.
/// </summary>
bool IsMouseOverScrollbar
{
get
{
Point ptMouse = MouseUtilities.GetMousePosition( this.listView );
HitTestResult res = VisualTreeHelper.HitTest( this.listView, ptMouse );
if( res == null )
return false;
DependencyObject depObj = res.VisualHit;
while( depObj != null )
{
if( depObj is ScrollBar )
return true;
// VisualTreeHelper works with objects of type Visual or Visual3D.
// If the current object is not derived from Visual or Visual3D,
// then use the LogicalTreeHelper to find the parent element.
if( depObj is Visual || depObj is System.Windows.Media.Media3D.Visual3D )
depObj = VisualTreeHelper.GetParent( depObj );
else
depObj = LogicalTreeHelper.GetParent( depObj );
}
return false;
}
}
#endregion // IsMouseOverScrollbar
#region ItemUnderDragCursor
ItemType ItemUnderDragCursor
{
get { return this.itemUnderDragCursor; }
set
{
if( this.itemUnderDragCursor == value )
return;
// The first pass handles the previous item under the cursor.
// The second pass handles the new one.
for( int i = 0; i < 2; ++i )
{
if( i == 1 )
this.itemUnderDragCursor = value;
if( this.itemUnderDragCursor != null )
{
ListViewItem listViewItem = this.GetListViewItem( this.itemUnderDragCursor );
if( listViewItem != null )
ListViewItemDragState.SetIsUnderDragCursor( listViewItem, i == 1 );
}
}
}
}
#endregion // ItemUnderDragCursor
#region PerformDragOperation
void PerformDragOperation()
{
ItemType selectedItem = this.listView.SelectedItem as ItemType;
DragDropEffects allowedEffects = DragDropEffects.Move | DragDropEffects.Move | DragDropEffects.Link;
if( DragDrop.DoDragDrop( this.listView, selectedItem, allowedEffects ) != DragDropEffects.None )
{
// The item was dropped into a new location,
// so make it the new selected item.
this.listView.SelectedItem = selectedItem;
}
}
#endregion // PerformDragOperation
#region ShowDragAdornerResolved
bool ShowDragAdornerResolved
{
get { return this.ShowDragAdorner && this.DragAdornerOpacity > 0.0; }
}
#endregion // ShowDragAdornerResolved
#region UpdateDragAdornerLocation
void UpdateDragAdornerLocation()
{
if( this.dragAdorner != null )
{
Point ptCursor = MouseUtilities.GetMousePosition( this.ListView );
double left = ptCursor.X - this.ptMouseDown.X;
// 4/13/2007 - Made the top offset relative to the item being dragged.
ListViewItem itemBeingDragged = this.GetListViewItem( this.indexToSelect );
Point itemLoc = itemBeingDragged.TranslatePoint( new Point( 0, 0 ), this.ListView );
double top = itemLoc.Y + ptCursor.Y - this.ptMouseDown.Y;
this.dragAdorner.SetOffsets( left, top );
}
}
#endregion // UpdateDragAdornerLocation
#endregion // Private Helpers
}
#endregion // ListViewDragDropManager
#region ListViewItemDragState
/// <summary>
/// Exposes attached properties used in conjunction with the ListViewDragDropManager class.
/// Those properties can be used to allow triggers to modify the appearance of ListViewItems
/// in a ListView during a drag-drop operation.
/// </summary>
public static class ListViewItemDragState
{
#region IsBeingDragged
/// <summary>
/// Identifies the ListViewItemDragState's IsBeingDragged attached property.
/// This field is read-only.
/// </summary>
public static readonly DependencyProperty IsBeingDraggedProperty =
DependencyProperty.RegisterAttached(
"IsBeingDragged",
typeof( bool ),
typeof( ListViewItemDragState ),
new UIPropertyMetadata( false ) );
/// <summary>
/// Returns true if the specified ListViewItem is being dragged, else false.
/// </summary>
/// <param name="item">The ListViewItem to check.</param>
public static bool GetIsBeingDragged( ListViewItem item )
{
return (bool)item.GetValue( IsBeingDraggedProperty );
}
/// <summary>
/// Sets the IsBeingDragged attached property for the specified ListViewItem.
/// </summary>
/// <param name="item">The ListViewItem to set the property on.</param>
/// <param name="value">Pass true if the element is being dragged, else false.</param>
internal static void SetIsBeingDragged( ListViewItem item, bool value )
{
item.SetValue( IsBeingDraggedProperty, value );
}
#endregion // IsBeingDragged
#region IsUnderDragCursor
/// <summary>
/// Identifies the ListViewItemDragState's IsUnderDragCursor attached property.
/// This field is read-only.
/// </summary>
public static readonly DependencyProperty IsUnderDragCursorProperty =
DependencyProperty.RegisterAttached(
"IsUnderDragCursor",
typeof( bool ),
typeof( ListViewItemDragState ),
new UIPropertyMetadata( false ) );
/// <summary>
/// Returns true if the specified ListViewItem is currently underneath the cursor
/// during a drag-drop operation, else false.
/// </summary>
/// <param name="item">The ListViewItem to check.</param>
public static bool GetIsUnderDragCursor( ListViewItem item )
{
return (bool)item.GetValue( IsUnderDragCursorProperty );
}
/// <summary>
/// Sets the IsUnderDragCursor attached property for the specified ListViewItem.
/// </summary>
/// <param name="item">The ListViewItem to set the property on.</param>
/// <param name="value">Pass true if the element is underneath the drag cursor, else false.</param>
internal static void SetIsUnderDragCursor( ListViewItem item, bool value )
{
item.SetValue( IsUnderDragCursorProperty, value );
}
#endregion // IsUnderDragCursor
}
#endregion // ListViewItemDragState
#region ProcessDropEventArgs
/// <summary>
/// Event arguments used by the ListViewDragDropManager.ProcessDrop event.
/// </summary>
/// <typeparam name="ItemType">The type of data object being dropped.</typeparam>
public class ProcessDropEventArgs<ItemType> : EventArgs where ItemType : class
{
#region Data
ObservableCollection<ItemType> itemsSource;
ItemType dataItem;
int oldIndex;
int newIndex;
DragDropEffects allowedEffects = DragDropEffects.None;
DragDropEffects effects = DragDropEffects.None;
#endregion // Data
#region Constructor
internal ProcessDropEventArgs(
ObservableCollection<ItemType> itemsSource,
ItemType dataItem,
int oldIndex,
int newIndex,
DragDropEffects allowedEffects )
{
this.itemsSource = itemsSource;
this.dataItem = dataItem;
this.oldIndex = oldIndex;
this.newIndex = newIndex;
this.allowedEffects = allowedEffects;
}
#endregion // Constructor
#region Public Properties
/// <summary>
/// The items source of the ListView where the drop occurred.
/// </summary>
public ObservableCollection<ItemType> ItemsSource
{
get { return this.itemsSource; }
}
/// <summary>
/// The data object which was dropped.
/// </summary>
public ItemType DataItem
{
get { return this.dataItem; }
}
/// <summary>
/// The current index of the data item being dropped, in the ItemsSource collection.
/// </summary>
public int OldIndex
{
get { return this.oldIndex; }
}
/// <summary>
/// The target index of the data item being dropped, in the ItemsSource collection.
/// </summary>
public int NewIndex
{
get { return this.newIndex; }
}
/// <summary>
/// The drag drop effects allowed to be performed.
/// </summary>
public DragDropEffects AllowedEffects
{
get { return allowedEffects; }
}
/// <summary>
/// The drag drop effect(s) performed on the dropped item.
/// </summary>
public DragDropEffects Effects
{
get { return effects; }
set { effects = value; }
}
#endregion // Public Properties
}
#endregion // ProcessDropEventArgs
}