1
1
using System ;
2
2
using System . Collections ;
3
3
using System . Collections . Generic ;
4
+ using System . Collections . ObjectModel ;
4
5
using System . ComponentModel ;
5
6
using System . Linq ;
6
7
using System . Windows ;
@@ -181,23 +182,27 @@ public virtual void Drop(IDropInfo dropInfo)
181
182
182
183
var destinationList = dropInfo . TargetCollection . TryGetList ( ) ;
183
184
var data = ExtractData ( dropInfo . Data ) . OfType < object > ( ) . ToList ( ) ;
184
-
185
+ bool forceMoveBehavior = false ;
185
186
var copyData = ShouldCopyData ( dropInfo ) ;
186
187
if ( ! copyData )
187
188
{
188
189
var sourceList = dropInfo . DragInfo . SourceCollection . TryGetList ( ) ;
189
190
if ( sourceList != null )
190
191
{
191
- foreach ( var o in data )
192
+ forceMoveBehavior = SameObservableCollection ( sourceList , destinationList ) ;
193
+ if ( ! forceMoveBehavior )
192
194
{
193
- var index = sourceList . IndexOf ( o ) ;
194
- if ( index != - 1 )
195
+ foreach ( var o in data )
195
196
{
196
- sourceList . RemoveAt ( index ) ;
197
- // so, is the source list the destination list too ?
198
- if ( destinationList != null && Equals ( sourceList , destinationList ) && index < insertIndex )
197
+ var index = sourceList . IndexOf ( o ) ;
198
+ if ( index != - 1 )
199
199
{
200
- -- insertIndex ;
200
+ sourceList . RemoveAt ( index ) ;
201
+ // so, is the source list the destination list too ?
202
+ if ( destinationList != null && Equals ( sourceList , destinationList ) && index < insertIndex )
203
+ {
204
+ -- insertIndex ;
205
+ }
201
206
}
202
207
}
203
208
}
@@ -223,7 +228,19 @@ public virtual void Drop(IDropInfo dropInfo)
223
228
}
224
229
225
230
objects2Insert . Add ( obj2Insert ) ;
226
- destinationList . Insert ( insertIndex ++ , obj2Insert ) ;
231
+ if ( ! cloneData && forceMoveBehavior )
232
+ {
233
+ int index = destinationList . IndexOf ( o ) ;
234
+ if ( insertIndex > index )
235
+ {
236
+ insertIndex -- ;
237
+ }
238
+ Move ( destinationList , index , insertIndex ++ ) ;
239
+ }
240
+ else
241
+ {
242
+ destinationList . Insert ( insertIndex ++ , obj2Insert ) ;
243
+ }
227
244
}
228
245
229
246
var selectDroppedItems = itemsControl is TabControl || ( itemsControl != null && DragDrop . GetSelectDroppedItems ( itemsControl ) ) ;
@@ -234,6 +251,28 @@ public virtual void Drop(IDropInfo dropInfo)
234
251
}
235
252
}
236
253
254
+ private static void Move ( IList list , int sourceIndex , int destinationIndex )
255
+ {
256
+ if ( ! IsObservableCollection ( list ) )
257
+ throw new ArgumentException ( "ObservableCollection<T> was expected" , nameof ( list ) ) ;
258
+ var method = list . GetType ( ) . GetMethod ( "Move" ,
259
+ System . Reflection . BindingFlags . Instance
260
+ | System . Reflection . BindingFlags . Public ) ;
261
+ method . Invoke ( list , new object [ ] { sourceIndex , destinationIndex } ) ;
262
+ }
263
+
264
+ private static bool SameObservableCollection ( IList collection1 , IList collection2 )
265
+ {
266
+ return ReferenceEquals ( collection1 , collection2 ) && IsObservableCollection ( collection1 ) ;
267
+ }
268
+
269
+ private static bool IsObservableCollection ( IList collection )
270
+ {
271
+ return
272
+ collection . GetType ( ) . IsGenericType &&
273
+ collection . GetType ( ) . GetGenericTypeDefinition ( ) == typeof ( ObservableCollection < > ) ;
274
+ }
275
+
237
276
protected static bool IsChildOf ( UIElement targetItem , UIElement sourceItem )
238
277
{
239
278
var parent = ItemsControl . ItemsControlFromItemContainer ( targetItem ) ;
0 commit comments