void OnDragOver(object sender, DragEventArgs e)
        {
            // drag over is the only event which returns the position
            // GiveFeedback returns nonsense even from Mouse.GetPosition
            Point point = e.GetPosition(TreeView);

            if (TryScroll(point))
            {
                return;
            }

            if (dragAdorner == null)//external drop
            {
                var content = new DragContent();
                content.Add(e.Data);
                dragAdorner = new DragAdorner(TreeView, content);
            }

            dragAdorner.UpdatePosition(point);
            if (IsMouseOverAdorner(point))
            {
                return;
            }
            var itemsPresenter = TreeView.ScrollViewer.Content as ItemsPresenter;

            if (itemsPresenter.InputHitTest(e.GetPosition(itemsPresenter)) == null)
            {
                dragAdorner.Content.CanDrop   = false;
                dragAdorner.Content.CanInsert = false;
                //dragAdorner.Content.InsertIndex = -1;
                if (insertAdorner != null)
                {
                    insertAdorner.Dispose();
                }
                return;
            }

            if (itemMouseIsOver != null)
            {
                itemMouseIsOver.IsCurrentDropTarget = false;
            }

            itemMouseIsOver = GetTreeViewItemUnderMouse(point);
            if (itemMouseIsOver == null)
            {
                return;
            }
            CanInsertReturn canInsertReturn = CanInsert(itemMouseIsOver, e.GetPosition, e.Data);

            if (canInsertReturn != null)
            {
                dragAdorner.Content.CanDrop   = false;
                dragAdorner.Content.CanInsert = true;
                //dragAdorner.Content.InsertIndex = canInsertReturn.Index;

                if (insertAdorner == null)
                {
                    insertAdorner = new InsertAdorner(itemMouseIsOver, new InsertContent {
                        Before = canInsertReturn.Before
                    });
                }
                else
                {
                    insertAdorner.Dispose();
                    insertAdorner = new InsertAdorner(itemMouseIsOver, new InsertContent {
                        Before = canInsertReturn.Before
                    });
                }

                itemMouseIsOver.IsCurrentDropTarget = false;
            }
            else
            {
                dragAdorner.Content.CanInsert = false;
                //dragAdorner.Content.InsertIndex = -1;
                if (insertAdorner != null)
                {
                    insertAdorner.Dispose();
                    insertAdorner = null;
                }

                dragAdorner.Content.CanDrop = CanDrop(itemMouseIsOver, e.Data) != null;
                if (itemMouseIsOver != null)
                {
                    itemMouseIsOver.IsCurrentDropTarget = true;
                }
            }
        }
 private void DragStart(DragContent dragData)
 {
     initialCursor = TreeView.Cursor;
     autoScroller.IsEnabled = true;
     if (dragAdorner == null)
     {
         dragAdorner = new DragAdorner(TreeView, dragData);
     }
 }
        void OnDragOver(object sender, DragEventArgs e)
        {
            // drag over is the only event which returns the position
            // GiveFeedback returns nonsense even from Mouse.GetPosition
            Point point = e.GetPosition(TreeView);

            if (TryScroll(point)) return;

            if (dragAdorner == null)//external drop
            {
                var content = new DragContent();
                content.Add(e.Data);
                dragAdorner = new DragAdorner(TreeView, content);
            }

            dragAdorner.UpdatePosition(point);
            if (IsMouseOverAdorner(point)) return;
            var itemsPresenter = TreeView.ScrollViewer.Content as ItemsPresenter;
            if (itemsPresenter.InputHitTest(e.GetPosition(itemsPresenter)) == null)
            {
                dragAdorner.Content.CanDrop = false;
                dragAdorner.Content.CanInsert = false;
                dragAdorner.Content.InsertIndex = -1;
                if (insertAdorner != null) insertAdorner.Dispose();
                return;
            }

            if (itemMouseIsOver != null)
            {
                itemMouseIsOver.IsCurrentDropTarget = false;
            }

            itemMouseIsOver = GetTreeViewItemUnderMouse(point);
            if (itemMouseIsOver == null) return;
            CanInsertReturn canInsertReturn = CanInsert(itemMouseIsOver, e.GetPosition, e.Data);
            if (canInsertReturn != null)
            {
                dragAdorner.Content.CanDrop = false;
                dragAdorner.Content.CanInsert = true;
                dragAdorner.Content.InsertIndex = canInsertReturn.Index;

                if (insertAdorner == null)
                {
                    insertAdorner = new InsertAdorner(itemMouseIsOver, new InsertContent { Before = canInsertReturn.Before });
                }
                else
                {
                    insertAdorner.Dispose();
                    insertAdorner = new InsertAdorner(itemMouseIsOver, new InsertContent { Before = canInsertReturn.Before });
                }

                itemMouseIsOver.IsCurrentDropTarget = false;
            }
            else
            {
                dragAdorner.Content.CanInsert = false;
                dragAdorner.Content.InsertIndex = -1;
                if (insertAdorner != null)
                {
                    insertAdorner.Dispose();
                    insertAdorner = null;
                }

                dragAdorner.Content.CanDrop = CanDrop(itemMouseIsOver, e.Data) != null;
                if (itemMouseIsOver != null)
                {
                    itemMouseIsOver.IsCurrentDropTarget = true;
                }
            }
        }
        private void CleanUpAdorners()
        {
            if (dragAdorner != null)
            {
                // on external drop no cleanup method is called.
                dragAdorner.Dispose();
                dragAdorner = null;
            }

            if (insertAdorner != null)
            {
                insertAdorner.Dispose();
                insertAdorner = null;
            }
        }