/// <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. /// /// Note that only TreeViewItems with no children can be moved. /// </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) { TreeViewDataProvider <TSourceContainer, TSourceObject> dataProvider = this.GetData(e) as TreeViewDataProvider <TSourceContainer, TSourceObject>; if (dataProvider != null) { TSourceObject dragSourceObject = dataProvider.SourceObject as TSourceObject; TSourceContainer dragSourceContainer = dataProvider.SourceContainer as TSourceContainer; Debug.Assert(dragSourceObject != null); Debug.Assert(dragSourceContainer != null); ListBox dropContainer = Utilities.FindParentControlIncludingMe <ListBox>(e.Source as DependencyObject); ListBoxItem dropTarget = Utilities.FindParentControlIncludingMe <ListBoxItem>(e.Source as DependencyObject); if (!dragSourceObject.HasItems) // TreeViewItem must be a leaf { if (bDrop) { dataProvider.Unparent(); ListBoxItem item = new ListBoxItem(); item.Content = dragSourceObject.Header; item.ToolTip = dragSourceObject.ToolTip; if (dropTarget == null) { dropContainer.Items.Add(item); } else { dropContainer.Items.Insert(dropContainer.Items.IndexOf(dropTarget), item); } item.IsSelected = true; item.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. /// /// Note that the source object cannot be an ancestor of the drop target. /// </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) { TreeViewDataProvider <TContainer, TObject> dataProvider = this.GetData(e) as TreeViewDataProvider <TContainer, TObject>; if (dataProvider != null) { TContainer dragSourceContainer = dataProvider.SourceContainer as TContainer; TreeViewItem dragSourceObject = dataProvider.SourceObject as TreeViewItem; Debug.Assert(dragSourceContainer != null); Debug.Assert(dragSourceObject != null); TContainer dropContainer = Utilities.FindParentControlIncludingMe <TContainer>(sender as DependencyObject); Debug.Assert(dropContainer != null); TObject dropTarget = e.Source as TObject; if (dropTarget == null) { if (bDrop) { dataProvider.Unparent(); dropContainer.Items.Add(dragSourceObject); } e.Effects = DragDropEffects.Move; e.Handled = true; #if PRINT2OUTPUT Debug.WriteLine(" Move0"); #endif } else { bool IsAncestor = dragSourceObject.IsAncestorOf(dropTarget); if ((dataProvider.KeyStates & DragDropKeyStates.ShiftKey) != 0) { ItemsControl shiftDropTarget = Utilities.FindParentControlExcludingMe <ItemsControl>(dropTarget); Debug.Assert(shiftDropTarget != null); if (!IsAncestor) { if (bDrop) { dataProvider.Unparent(); Debug.Assert(shiftDropTarget != null); shiftDropTarget.Items.Insert(shiftDropTarget.Items.IndexOf(dropTarget), dragSourceObject); } e.Effects = DragDropEffects.Link; e.Handled = true; #if PRINT2OUTPUT Debug.WriteLine(" Link1"); #endif } else { e.Effects = DragDropEffects.None; e.Handled = true; #if PRINT2OUTPUT Debug.WriteLine(" None1"); #endif } } else { if (!IsAncestor && (dragSourceObject != dropTarget)) { if (bDrop) { dataProvider.Unparent(); dropTarget.Items.Add(dragSourceObject); } e.Effects = DragDropEffects.Move; e.Handled = true; #if PRINT2OUTPUT Debug.WriteLine(" Move2"); #endif } else { e.Effects = DragDropEffects.None; e.Handled = true; #if PRINT2OUTPUT Debug.WriteLine(" None2"); #endif } } } if (bDrop && e.Handled && (e.Effects != DragDropEffects.None)) { dragSourceObject.IsSelected = true; dragSourceObject.BringIntoView(); } } }