示例#1
0
        public void Drop(IDropInfo dropInfo)
        {
            bool stringFlag = false;

            if (dropInfo.DragInfo == null && dropInfo.Data is IToolboxItem item && dropInfo.IsSameDragDropContextAsSource)
            {
                if (item.DataSource.Data.Format != DataFormats.Text)
                {
                    return;
                }
                stringFlag = true;
            }
            if (dropInfo.IsSameDragDropContextAsSource && !stringFlag)
            {
                //Restore original data
                dropInfo.Data = dropInfo.DragInfo.SourceItem;
            }
            if (stringFlag)
            {
                var destinationList = dropInfo.TargetCollection.TryGetList();
                if (destinationList != null)
                {
                    destinationList.Insert(dropInfo.InsertIndex, dropInfo.Data);

                    var selectDroppedItems = DragDrop.DragDrop.GetSelectDroppedItems(dropInfo.VisualTarget);
                    if (selectDroppedItems)
                    {
                        DefaultDropHandler.SelectDroppedItems(dropInfo, new List <object> {
                            dropInfo.Data
                        });
                    }
                }
            }
            else
            {
                DragDrop.DragDrop.DefaultDropHandler.Drop(dropInfo);
            }
        }
        /// <inheritdoc />
        public void Drop(IDropInfo dropInfo)
        {
            var wrapper = GetSerializableWrapper(dropInfo);

            if (wrapper == null || dropInfo.TargetCollection == null)
            {
                return;
            }

            // at this point the drag info can be null, cause the other app doesn't know it

            var  insertIndex      = dropInfo.UnfilteredInsertIndex;
            var  destinationList  = dropInfo.TargetCollection.TryGetList();
            var  data             = wrapper.Items.ToList();
            bool isSameCollection = false;

            var copyData = ShouldCopyData(dropInfo, wrapper.DragDropCopyKeyState);

            if (!copyData)
            {
                var sourceList = dropInfo.DragInfo?.SourceCollection?.TryGetList();
                if (sourceList != null)
                {
                    isSameCollection = sourceList.IsSameObservableCollection(destinationList);
                    if (!isSameCollection)
                    {
                        foreach (var o in data)
                        {
                            var index = sourceList.IndexOf(o);
                            if (index != -1)
                            {
                                sourceList.RemoveAt(index);

                                // so, is the source list the destination list too ?
                                if (destinationList != null && Equals(sourceList, destinationList) && index < insertIndex)
                                {
                                    --insertIndex;
                                }
                            }
                        }
                    }
                }
            }

            if (destinationList != null)
            {
                var objects2Insert = new List <object>();

                // check for cloning
                var cloneData = dropInfo.Effects.HasFlag(DragDropEffects.Copy) || dropInfo.Effects.HasFlag(DragDropEffects.Link);

                foreach (var o in data)
                {
                    var obj2Insert = o;
                    if (cloneData)
                    {
                        if (o is ICloneable cloneable)
                        {
                            obj2Insert = cloneable.Clone();
                        }
                    }

                    objects2Insert.Add(obj2Insert);

                    if (!cloneData && isSameCollection)
                    {
                        var index = destinationList.IndexOf(o);
                        if (index != -1)
                        {
                            if (insertIndex > index)
                            {
                                insertIndex--;
                            }

                            Move(destinationList, index, insertIndex++);
                        }
                    }
                    else
                    {
                        destinationList.Insert(insertIndex++, obj2Insert);
                    }
                }

                DefaultDropHandler.SelectDroppedItems(dropInfo, objects2Insert);
            }
        }