/// <summary>
        /// Gets the drag handler from the drag info or from the sender, if the drag info is null
        /// </summary>
        /// <param name="dragInfo">the drag info object</param>
        /// <param name="sender">the sender from an event, e.g. mouse down, mouse move</param>
        /// <returns></returns>
        private static IDragSource TryGetDragHandler(DragInfo dragInfo, UIElement sender)
        {
            IDragSource dragHandler = null;

            if (dragInfo != null && dragInfo.VisualSource != null)
            {
                dragHandler = GetDragHandler(dragInfo.VisualSource);
            }
            if (dragHandler == null && sender != null)
            {
                dragHandler = GetDragHandler(sender);
            }
            return(dragHandler ?? DefaultDragHandler);
        }
        private static void DragSource_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            // Ignore the click if clickCount != 1 or the user has clicked on a scrollbar.
            var elementPosition = e.GetPosition((IInputElement)sender);

            if (e.ClickCount != 1 ||
                (sender as UIElement).IsDragSourceIgnored() ||
                (e.Source as UIElement).IsDragSourceIgnored() ||
                (e.OriginalSource as UIElement).IsDragSourceIgnored() ||
                (sender is TabControl) && !HitTestUtilities.HitTest4Type <TabPanel>(sender, elementPosition) ||
                HitTestUtilities.HitTest4Type <RangeBase>(sender, elementPosition) ||
                HitTestUtilities.HitTest4Type <ButtonBase>(sender, elementPosition) ||
                HitTestUtilities.HitTest4Type <TextBoxBase>(sender, elementPosition) ||
                HitTestUtilities.HitTest4Type <PasswordBox>(sender, elementPosition) ||
                HitTestUtilities.HitTest4Type <ComboBox>(sender, elementPosition) ||
                HitTestUtilities.HitTest4GridViewColumnHeader(sender, elementPosition) ||
                HitTestUtilities.HitTest4DataGridTypes(sender, elementPosition) ||
                HitTestUtilities.IsNotPartOfSender(sender, e))
            {
                m_DragInfo = null;
                return;
            }

            m_DragInfo = new DragInfo(sender, e);

            var dragHandler = TryGetDragHandler(m_DragInfo, sender as UIElement);

            if (!dragHandler.CanStartDrag(m_DragInfo))
            {
                m_DragInfo = null;
                return;
            }

            // If the sender is a list box that allows multiple selections, ensure that clicking on an
            // already selected item does not change the selection, otherwise dragging multiple items
            // is made impossible.
            var itemsControl = sender as ItemsControl;

            if ((Keyboard.Modifiers & ModifierKeys.Shift) == 0 && (Keyboard.Modifiers & ModifierKeys.Control) == 0 && m_DragInfo.VisualSourceItem != null && itemsControl != null && itemsControl.CanSelectMultipleItems())
            {
                var selectedItems = itemsControl.GetSelectedItems().OfType <object>().ToList();
                if (selectedItems.Count > 1 && selectedItems.Contains(m_DragInfo.SourceItem))
                {
                    m_ClickSupressItem = m_DragInfo.SourceItem;
                    e.Handled          = true;
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// Initializes a new instance of the DropInfo class.
        /// </summary>
        ///
        /// <param name="sender">
        /// The sender of the drag event.
        /// </param>
        ///
        /// <param name="e">
        /// The drag event.
        /// </param>
        ///
        /// <param name="dragInfo">
        /// Information about the source of the drag, if the drag came from within the framework.
        /// </param>
        public DropInfo(object sender, DragEventArgs e, DragInfo dragInfo)
        {
            var dataFormat = DragDrop.DataFormat.Name;

            this.Data      = (e.Data.GetDataPresent(dataFormat)) ? e.Data.GetData(dataFormat) : e.Data;
            this.DragInfo  = dragInfo;
            this.KeyStates = e.KeyStates;

            this.VisualTarget = sender as UIElement;
            // if there is no drop target, find another
            if (!this.VisualTarget.IsDropTarget())
            {
                // try to find next element
                var element = this.VisualTarget.TryGetNextAncestorDropTargetElement();
                if (element != null)
                {
                    this.VisualTarget = element;
                }
            }
            // visual target can be null, so give us a point...
            this.DropPosition = this.VisualTarget != null?e.GetPosition(this.VisualTarget) : new Point();

            if (this.VisualTarget is TabControl)
            {
                if (!HitTestUtilities.HitTest4Type <TabPanel>(this.VisualTarget, this.DropPosition))
                {
                    return;
                }
            }

            if (this.VisualTarget is ItemsControl)
            {
                var itemsControl = (ItemsControl)this.VisualTarget;
                item = itemsControl.GetItemContainerAt(this.DropPosition);
                var directlyOverItem = item != null;

                this.TargetGroup               = itemsControl.FindGroup(this.DropPosition);
                this.VisualTargetOrientation   = itemsControl.GetItemsPanelOrientation();
                this.VisualTargetFlowDirection = itemsControl.GetItemsPanelFlowDirection();

                if (item == null)
                {
                    item             = itemsControl.GetItemContainerAt(this.DropPosition, this.VisualTargetOrientation);
                    directlyOverItem = false;
                }

                if (item == null && this.TargetGroup != null && this.TargetGroup.IsBottomLevel)
                {
                    var itemData = this.TargetGroup.Items.FirstOrDefault();
                    if (itemData != null)
                    {
                        item             = itemsControl.ItemContainerGenerator.ContainerFromItem(itemData) as UIElement;
                        directlyOverItem = false;
                    }
                }

                if (item != null)
                {
                    itemParent = ItemsControl.ItemsControlFromItemContainer(item);

                    this.InsertIndex      = itemParent.ItemContainerGenerator.IndexFromContainer(item);
                    this.TargetCollection = itemParent.ItemsSource ?? itemParent.Items;

                    var tvItem = item as TreeViewItem;

                    if (directlyOverItem || tvItem != null)
                    {
                        this.TargetItem       = itemParent.ItemContainerGenerator.ItemFromContainer(item);
                        this.VisualTargetItem = item;
                    }

                    var itemRenderSize = item.RenderSize;

                    if (this.VisualTargetOrientation == Orientation.Vertical)
                    {
                        var currentYPos  = e.GetPosition(item).Y;
                        var targetHeight = itemRenderSize.Height;

                        if (currentYPos > targetHeight / 2)
                        {
                            this.InsertIndex++;
                            this.InsertPosition = RelativeInsertPosition.AfterTargetItem;
                        }
                        else
                        {
                            this.InsertPosition = RelativeInsertPosition.BeforeTargetItem;
                        }

                        if (currentYPos > targetHeight * 0.25 && currentYPos < targetHeight * 0.75)
                        {
                            if (tvItem != null)
                            {
                                this.TargetCollection = tvItem.ItemsSource ?? tvItem.Items;
                                this.InsertIndex      = this.TargetCollection != null?this.TargetCollection.OfType <object>().Count() : 0;
                            }
                            this.InsertPosition |= RelativeInsertPosition.TargetItemCenter;
                        }
                        //System.Diagnostics.Debug.WriteLine("==> DropInfo: {0}, {1}, {2}, Y={3}", this.InsertPosition, item, this.InsertIndex, currentYPos);
                    }
                    else
                    {
                        var currentXPos = e.GetPosition(item).X;
                        var targetWidth = itemRenderSize.Width;

                        if (this.VisualTargetFlowDirection == FlowDirection.RightToLeft)
                        {
                            if (currentXPos > targetWidth / 2)
                            {
                                this.InsertPosition = RelativeInsertPosition.BeforeTargetItem;
                            }
                            else
                            {
                                this.InsertIndex++;
                                this.InsertPosition = RelativeInsertPosition.AfterTargetItem;
                            }
                        }
                        else if (this.VisualTargetFlowDirection == FlowDirection.LeftToRight)
                        {
                            if (currentXPos > targetWidth / 2)
                            {
                                this.InsertIndex++;
                                this.InsertPosition = RelativeInsertPosition.AfterTargetItem;
                            }
                            else
                            {
                                this.InsertPosition = RelativeInsertPosition.BeforeTargetItem;
                            }
                        }

                        if (currentXPos > targetWidth * 0.25 && currentXPos < targetWidth * 0.75)
                        {
                            if (tvItem != null)
                            {
                                this.TargetCollection = tvItem.ItemsSource ?? tvItem.Items;
                                this.InsertIndex      = this.TargetCollection != null?this.TargetCollection.OfType <object>().Count() : 0;
                            }
                            this.InsertPosition |= RelativeInsertPosition.TargetItemCenter;
                        }
                        //System.Diagnostics.Debug.WriteLine("==> DropInfo: InsPos={0}, InsIndex={1}, X={2}, Item={3}", this.InsertPosition, this.InsertIndex, currentXPos, item);
                    }
                }
                else
                {
                    this.TargetCollection = itemsControl.ItemsSource ?? itemsControl.Items;
                    this.InsertIndex      = itemsControl.Items.Count;
                    //System.Diagnostics.Debug.WriteLine("==> DropInfo: {0}, item=NULL, {1}", this.InsertPosition, this.InsertIndex);
                }
            }
            else
            {
                this.VisualTargetItem = this.VisualTarget;
            }
        }