Beispiel #1
0
        private static void DropTarget_PreviewDragOver(object sender, DragEventArgs e)
        {
            var elementPosition = e.GetPosition((IInputElement)sender);
            if (HitTestUtilities.HitTest4Type<ScrollBar>(sender, elementPosition)
                || HitTestUtilities.HitTest4GridViewColumnHeader(sender, elementPosition)
                || HitTestUtilities.HitTest4DataGridTypesOnDragOver(sender, elementPosition))
            {
                e.Effects = DragDropEffects.None;
                e.Handled = true;
                return;
            }

            var dropInfo = new DropInfo(sender, e, _dragInfo);
            var dropHandler = GetDropHandler((UIElement)sender);

            if (dropHandler == null)
            {
                // sender does not have DropHandler attached property set.
                // use default drop target in that case

                dropHandler = DefaultDropTarget;
            }
            var itemsControl = dropInfo.VisualTarget;

            dropHandler.DragOver(dropInfo);

            if (DragAdorner == null && _dragInfo != null)
            {
                CreateDragAdorner();
            }

            if (DragAdorner != null)
            {
                var tempAdornerPos = e.GetPosition(DragAdorner.AdornedElement);

                if (tempAdornerPos.X > 0 && tempAdornerPos.Y > 0)
                {
                    _adornerPos = tempAdornerPos;
                }

                if (DragAdorner.RenderSize.Width > 0 && DragAdorner.RenderSize.Height > 0)
                {
                    _adornerSize = DragAdorner.RenderSize;
                }

                if (_dragInfo != null)
                {
                    // move the adorner
                    var offsetX = _adornerSize.Width * -GetDragMouseAnchorPoint(_dragInfo.VisualSource).X;
                    var offsetY = _adornerSize.Height * -GetDragMouseAnchorPoint(_dragInfo.VisualSource).Y;
                    _adornerPos.Offset(offsetX, offsetY);
                    var maxAdornerPosX = DragAdorner.AdornedElement.RenderSize.Width;
                    var adornerPosRightX = (_adornerPos.X + _adornerSize.Width);
                    if (adornerPosRightX > maxAdornerPosX)
                    {
                        _adornerPos.Offset(-adornerPosRightX + maxAdornerPosX, 0);
                    }
                }

                DragAdorner.MousePosition = _adornerPos;
                DragAdorner.InvalidateVisual();
            }

            // If the target is an ItemsControl then update the drop target adorner.
            if (itemsControl != null)
            {
                // Display the adorner in the control's ItemsPresenter. If there is no 
                // ItemsPresenter provided by the style, try getting hold of a
                // ScrollContentPresenter and using that.
                var adornedElement =
                  (UIElement)itemsControl.FindVisualDescendant<ItemsPresenter>() ??
                  (UIElement)itemsControl.FindVisualDescendant<ScrollContentPresenter>();

                if (adornedElement != null)
                {
                    if (dropInfo.DropTargetAdorner == null)
                    {
                        DropTargetAdorner = null;
                    }
                    else if (!dropInfo.DropTargetAdorner.IsInstanceOfType(DropTargetAdorner))
                    {
                        DropTargetAdorner = DropTargetAdorner.Create(dropInfo.DropTargetAdorner, adornedElement);
                    }

                    if (DropTargetAdorner != null)
                    {
                        DropTargetAdorner.DropInfo = dropInfo;
                        DropTargetAdorner.InvalidateVisual();
                    }
                }
            }

            // Set the drag effect adorner if there is one
            if (EffectAdorner == null && _dragInfo != null)
            {
                CreateEffectAdorner(dropInfo);
            }

            if (EffectAdorner != null)
            {
                var adornerPos = e.GetPosition(EffectAdorner.AdornedElement);
                adornerPos.Offset(20, 20);
                EffectAdorner.MousePosition = adornerPos;
                EffectAdorner.InvalidateVisual();
            }

            e.Effects = dropInfo.AllowedEffects;
            e.Handled = true;

            if(e.Effects != DragDropEffects.None)
                Scroll((DependencyObject)dropInfo.VisualTarget, e);
        }
Beispiel #2
0
        private static void DropTarget_PreviewDrop(object sender, DragEventArgs e)
        {
            var local_dragInfo = _dragInfo;

            if (local_dragInfo == null)
            {
                //NOTE: internal drag info may be null if drag operation did not originate from control using this DragDrop implementation
                //      (e.g. outside of wpf app)
                //      In that case convert DragEventArgs to DragInfo representation and then use it

                local_dragInfo = DragInfo.CreateFromEvent(sender, e);
            }

            var dropInfo = new DropInfo(sender, e, local_dragInfo);
            var dropHandler = GetDropHandler((UIElement)sender) ?? DefaultDropTarget;
            var dragHandler = GetDragSource(local_dragInfo, sender as UIElement);

            DragAdorner = null;
            EffectAdorner = null;
            DropTargetAdorner = null;

            dropHandler.Drop(dropInfo);
            dragHandler.Dropped(dropInfo);

            Mouse.OverrideCursor = null;
            e.Handled = true;
        }
Beispiel #3
0
        private static void CreateEffectAdorner(DropInfo dropInfo)
        {
            var template = GetEffectAdornerTemplate(dropInfo.VisualTarget, dropInfo.AllowedEffects, dropInfo.DestinationText);

            if (template != null)
            {
                var rootElement = (UIElement)Application.Current.MainWindow.Content;
                UIElement adornment = null;

                var contentPresenter = new ContentPresenter();
                contentPresenter.Content = _dragInfo.Data;
                contentPresenter.ContentTemplate = template;

                adornment = contentPresenter;

                EffectAdorner = new DragAdorner(rootElement, adornment);
            }
        }