コード例 #1
0
        /// <summary>
        /// Drop started
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void PreviewDrop(object sender, DragEventArgs e)
        {
            var itemsControl = (ItemsControl)sender;

            this.DestroyAdorners();
            e.Handled = true;
            if (e.Data.GetDataPresent(this.ItemTypeKey))
            {
                int index = FindInsertionIndex(itemsControl, e);

                DragInfo data = e.Data.GetData(this.ItemTypeKey) as DragInfo;
                if (data != null && (!data.AllowOnlySelf || itemsControl == data.Source))
                {
                    var allowedEffects = e.AllowedEffects;
                    if (this.DropInitiated != null)
                    {
                        DragDropEventArgs de = new DragDropEventArgs(data.Source, itemsControl, data.Data, index)
                        {
                            AllowedEffects = allowedEffects
                        };
                        this.DropInitiated(this, de);
                        allowedEffects = de.AllowedEffects;
                        if (de.Cancel)
                        {
                            e.Effects = DragDropEffects.None;
                            return;
                        }
                    }

                    object itemToAdd = data.Data;
                    e.Effects = GetDropEffectType(allowedEffects, e.KeyStates);

                    if (DragUtilities.DoesItemExists(itemsControl, itemToAdd) &&
                        (e.Effects == DragDropEffects.Move ||
                         (e.Effects & DragDropEffects.Move) > 0 && (e.KeyStates & DragDropKeyStates.ControlKey) > 0))
                    {
                        DragUtilities.RemoveItem(itemsControl, itemToAdd);
                        // Recalc our position based on the removal
                        index = FindInsertionIndex(itemsControl, e);
                    }

                    DragUtilities.AddItem(itemsControl, itemToAdd, index);
                    DragUtilities.SelectItem(itemsControl, index);
                }
                else
                {
                    e.Effects = DragDropEffects.None;
                }
            }
            else
            {
                e.Effects = DragDropEffects.None;
            }
        }
コード例 #2
0
        /// <summary>
        /// This starts the drag operation from the given ItemsControl
        /// </summary>
        /// <param name="itemsControl"></param>
        private void DragStarted(ItemsControl itemsControl)
        {
            DragDropEffects allowedEffects = DragDropEffects.Copy | DragDropEffects.Move;

            if (this.DragInitiated != null)
            {
                var dde = new DragDropEventArgs(itemsControl, null, this._data)
                {
                    AllowedEffects = allowedEffects
                };
                this.DragInitiated(this, dde);
                if (dde.Cancel)
                {
                    this.ResetState();
                    return;
                }
                allowedEffects = dde.AllowedEffects;
            }

            UIElement draggedItemContainer = DragUtilities.GetItemContainerFromPoint(itemsControl, this._dragStartPosition);

            this._isDragging = true;

            DataObject dObject = new DataObject(this.ItemTypeKey, new DragInfo {
                Data = this._data, Source = itemsControl, AllowOnlySelf = this.AllowOnlySelf
            });
            DragDropEffects e = DragDrop.DoDragDrop(itemsControl, dObject, allowedEffects);

            if ((e & DragDropEffects.Move) != 0)
            {
                if (draggedItemContainer != null)
                {
                    int dragItemIndex = itemsControl.ItemContainerGenerator.IndexFromContainer(draggedItemContainer);
                    DragUtilities.RemoveItem(itemsControl, dragItemIndex);
                }
                else
                {
                    DragUtilities.RemoveItem(itemsControl, this._data);
                }
            }

            this.ResetState();
        }