Ejemplo n.º 1
0
        // DragSource

        private void DragSource_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.sourceItemsControl = (ItemsControl)sender;
            Visual visual = e.OriginalSource as Visual;

            this.topWindow            = (Window)DragDropUtilities.FindAncestor(typeof(Window), this.sourceItemsControl);
            this.initialMousePosition = e.GetPosition(this.topWindow);

            this.sourceItemContainer = DragDropUtilities.GetItemContainer(this.sourceItemsControl, visual);
            if (this.sourceItemContainer != null)
            {
                this.draggedData = this.sourceItemContainer.DataContext;
            }
            mouseDelta = e.GetPosition(sourceItemContainer);
        }
Ejemplo n.º 2
0
        private void DropTarget_PreviewDrop(object sender, DragEventArgs e)
        {
            if (isDragging)
            {
                object draggedItem  = e.Data.GetData(this.format.Name);
                int    indexRemoved = -1;

                if (draggedItem != null)
                {
                    if (sourceItemsControl == targetItemsControl)
                    {
                        if ((e.Effects & DragDropEffects.Move) != 0)
                        {
                            indexRemoved = DragDropUtilities.RemoveItemFromItemsControl(this.sourceItemsControl,
                                                                                        draggedItem);
                        }
                        // This happens when we drag an item to a later position within the same ItemsControl.
                        if (indexRemoved != -1 && this.sourceItemsControl == this.targetItemsControl &&
                            indexRemoved < this.insertionIndex)
                        {
                            this.insertionIndex--;
                        }
                        DragDropUtilities.InsertItemInItemsControl(this.targetItemsControl, draggedItem,
                                                                   this.insertionIndex);
                    }
                    else
                    {
                        if (draggedItem is ICloneable)
                        {
                            ICloneable cloneable = draggedItem as ICloneable;
                            DragDropUtilities.InsertItemInItemsControl(this.targetItemsControl, cloneable.Clone(),
                                                                       this.insertionIndex);
                        }
                        else
                        {
                            DragDropUtilities.InsertItemInItemsControl(this.targetItemsControl, draggedItem,
                                                                       this.insertionIndex);
                        }
                    }

                    RemoveDraggedAdorner();
                    RemoveInsertionAdorner();
                }
                e.Handled = true;
            }
        }
Ejemplo n.º 3
0
        // If the types of the dragged data and ItemsControl's source are compatible,
        // there are 3 situations to have into account when deciding the drop target:
        // 1. mouse is over an items container
        // 2. mouse is over the empty part of an ItemsControl, but ItemsControl is not empty
        // 3. mouse is over an empty ItemsControl.
        // The goal of this method is to decide on the values of the following properties:
        // targetItemContainer, insertionIndex and isInFirstHalf.
        private void DecideDropTarget(DragEventArgs e)
        {
            int    targetItemsControlCount = this.targetItemsControl.Items.Count;
            object draggedItem             = e.Data.GetData(this.format.Name);

            if (IsDropDataTypeAllowed(draggedItem))
            {
                if (targetItemsControlCount > 0)
                {
                    this.hasVerticalOrientation = DragDropUtilities.HasVerticalOrientation(this.targetItemsControl.ItemContainerGenerator.ContainerFromIndex(0) as FrameworkElement);
                    this.targetItemContainer    = DragDropUtilities.GetItemContainer(this.targetItemsControl, e.OriginalSource as Visual);

                    if (this.targetItemContainer != null)
                    {
                        Point positionRelativeToItemContainer = e.GetPosition(this.targetItemContainer);
                        this.isInFirstHalf  = DragDropUtilities.IsInFirstHalf(this.targetItemContainer, positionRelativeToItemContainer, this.hasVerticalOrientation);
                        this.insertionIndex = this.targetItemsControl.ItemContainerGenerator.IndexFromContainer(this.targetItemContainer);

                        if (!this.isInFirstHalf)
                        {
                            this.insertionIndex++;
                        }
                    }
                    else
                    {
                        this.targetItemContainer = this.targetItemsControl.ItemContainerGenerator.ContainerFromIndex(targetItemsControlCount - 1) as FrameworkElement;
                        this.isInFirstHalf       = false;
                        this.insertionIndex      = targetItemsControlCount;
                    }
                }
                else
                {
                    this.targetItemContainer = null;
                    this.insertionIndex      = 0;
                }
            }
            else
            {
                this.targetItemContainer = null;
                this.insertionIndex      = -1;
                e.Effects = DragDropEffects.None;
            }
        }
Ejemplo n.º 4
0
        // Drag = mouse down + move by a certain amount
        private void DragSource_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (this.draggedData != null && !isDragging)
            {
                // Only drag when user moved the mouse by a reasonable amount.
                if (DragDropUtilities.IsMovementBigEnough(this.initialMousePosition, e.GetPosition(this.topWindow)))
                {
                    isDragging = true;
                    DataObject data = new DataObject(this.format.Name, this.draggedData);

                    // Adding events to the window to make sure dragged adorner comes up when mouse is not over a drop target.
                    bool previousAllowDrop = this.topWindow.AllowDrop;
                    this.topWindow.AllowDrop  = true;
                    this.topWindow.DragEnter += TopWindow_DragEnter;
                    this.topWindow.DragOver  += TopWindow_DragOver;
                    this.topWindow.DragLeave += TopWindow_DragLeave;

                    DragDropEffects effects = System.Windows.DragDrop.DoDragDrop((DependencyObject)sender, data, DragDropEffects.Move);

                    isDragging = false;

                    // Without this call, there would be a bug in the following scenario: Click on a data item, and drag
                    // the mouse very fast outside of the window. When doing this really fast, for some reason I don't get
                    // the Window leave event, and the dragged adorner is left behind.
                    // With this call, the dragged adorner will disappear when we release the mouse outside of the window,
                    // which is when the DoDragDrop synchronous method returns.
                    RemoveDraggedAdorner();

                    this.topWindow.AllowDrop  = previousAllowDrop;
                    this.topWindow.DragEnter -= TopWindow_DragEnter;
                    this.topWindow.DragOver  -= TopWindow_DragOver;
                    this.topWindow.DragLeave -= TopWindow_DragLeave;

                    this.draggedData = null;
                }
            }
        }