/// <summary> /// First determine whether the drag data is supported. /// Finally handle the actual drop when <code>bDrop</code> is true. /// Insert the item before the drop target. When there is no drop /// target (dropped on empty space), add to the end of the items. /// </summary> /// <param name="bDrop">True to perform an actual drop, otherwise just return e.Effects</param> /// <param name="sender">DragDrop event <code>sender</code></param> /// <param name="e">DragDrop event arguments</param> private void DragOverOrDrop(bool bDrop, object sender, DragEventArgs e) { ListBoxDataProvider <TContainer, TObject> dataProvider = this.GetData(e) as ListBoxDataProvider <TContainer, TObject>; if (dataProvider != null) { TContainer dragSourceContainer = dataProvider.SourceContainer as TContainer; TObject dragSourceObject = dataProvider.SourceObject as TObject; Debug.Assert(dragSourceObject != null); Debug.Assert(dragSourceContainer != null); TContainer dropContainer = Utilities.FindParentControlIncludingMe <TContainer>(e.Source as DependencyObject); TObject dropTarget = e.Source as TObject; if (dropContainer != null) { if (bDrop) { dataProvider.Unparent(); if (dropTarget == null) { dropContainer.Items.Add(dragSourceObject); } else { dropContainer.Items.Insert(dropContainer.Items.IndexOf(dropTarget), dragSourceObject); } dragSourceObject.IsSelected = true; dragSourceObject.BringIntoView(); } e.Effects = DragDropEffects.Move; e.Handled = true; } else { e.Effects = DragDropEffects.None; e.Handled = true; } } }
/// <summary> /// First determine whether the drag data is supported. /// Finally handle the actual drop when <code>bDrop</code> is true. /// Add the item as the drop target's child when Shift is not pressed, /// or insert the item before the drop target when Shift is pressed. /// When there is no drop target (dropped on empty space), /// add to the end of the items. /// </summary> /// <param name="bDrop">True to perform an actual drop, otherwise just return e.Effects</param> /// <param name="sender">DragDrop event <code>sender</code></param> /// <param name="e">DragDrop event arguments</param> private void DragOverOrDrop(bool bDrop, object sender, DragEventArgs e) { ListBoxDataProvider <TSourceContainer, TSourceObject> dataProvider = this.GetData(e) as ListBoxDataProvider <TSourceContainer, TSourceObject>; if (dataProvider != null) { TSourceContainer dragSourceContainer = dataProvider.SourceContainer as TSourceContainer; TSourceObject dragSourceObject = dataProvider.SourceObject as TSourceObject; Debug.Assert(dragSourceContainer != null); Debug.Assert(dragSourceObject != null); ItemsControl dropContainer = Utilities.FindParentControlIncludingMe <ItemsControl>(sender as DependencyObject); Debug.Assert(dropContainer != null); TreeViewItem dropTarget = e.Source as TreeViewItem; TreeViewItem newTvi = null; if (bDrop) { dataProvider.Unparent(); newTvi = new TreeViewItem(); newTvi.Header = dragSourceObject.Content; } if (dropTarget == null) { if (bDrop) { dropContainer.Items.Add(newTvi); } e.Effects = DragDropEffects.Move; e.Handled = true; } else { if ((dataProvider.KeyStates & DragDropKeyStates.ShiftKey) != 0) // As sibling { if (bDrop) { ItemsControl shiftDropTarget = Utilities.FindParentControlExcludingMe <ItemsControl>(dropTarget); Debug.Assert(shiftDropTarget != null); shiftDropTarget.Items.Insert(shiftDropTarget.Items.IndexOf(dropTarget), newTvi); } e.Effects = DragDropEffects.Link; e.Handled = true; } else // As child { if (bDrop) { dropTarget.Items.Add(newTvi); } e.Effects = DragDropEffects.Move; e.Handled = true; } } if (bDrop) { newTvi.IsSelected = true; newTvi.BringIntoView(); } } }