示例#1
0
        protected override void OnExecute(object parameter)
        {
            if (!(parameter is IDataObject dataObject))
            {
                return;
            }

            IToolboxItem item;

            if (dataObject.GetFormats().Any(x => x == DataFormats.Text))
            {
                item = ToolboxItem.CreateTextItem(new ToolboxItemData(DataFormats.Text, dataObject.GetData(DataFormats.Text)));
            }
            else if (dataObject.GetDataPresent(ToolboxItemDataFormats.DataSource))
            {
                try
                {
                    var dataSource = dataObject.GetData(ToolboxItemDataFormats.DataSource) as ToolboxItemDefinitionBase;
                    item = new ToolboxItem(dataSource);
                }
                catch (ExternalException)
                {
                    return;
                }
            }
            else
            {
                return;
            }

            if (_toolbox.SelectedNode is IToolboxItem selectedItem)
            {
                var category = selectedItem.Parent;
                var index    = category.Items.IndexOf(selectedItem);
                category.Items.Insert(index + 1, item);
            }
            else if (_toolbox.SelectedNode is IToolboxCategory selectedCategory)
            {
                selectedCategory.Items.Insert(0, item);
            }
        }
示例#2
0
        public void DragOver(IDropInfo dropInfo)
        {
            dropInfo.DropTargetAdorner = null;
            var stringFlag = false;

            if (IsTextObjectInDragSource(dropInfo, out var dataObject))
            {
                dropInfo.Effects           = DragDropEffects.Copy | DragDropEffects.Move;
                dropInfo.DropTargetAdorner = typeof(ToolboxInsertAdorner);
                var format = dataObject.GetFormats().First();
                dropInfo.Data = ToolboxItem.CreateTextItem(new ToolboxItemData(format, dataObject.GetData(format)));
                stringFlag    = true;
            }

            if (dropInfo.IsSameDragDropContextAsSource && !stringFlag)
            {
                //Restore original data
                dropInfo.Data = dropInfo.DragInfo.SourceItem;
            }


            DragDrop.DragDrop.DefaultDropHandler.DragOver(dropInfo);

            if (!CanDropToolboxItem(dropInfo, dropInfo.Data as IToolboxNode))
            {
                dropInfo.Effects = DragDropEffects.None;
            }

            if (dropInfo.IsSameDragDropContextAsSource)
            {
                if (dropInfo.DragInfo != null && dropInfo.TargetItem == dropInfo.DragInfo.SourceItem)
                {
                    dropInfo.Effects = DragDropEffects.None;
                }
                else
                {
                    if (!stringFlag && Equals(dropInfo.TargetCollection, dropInfo.DragInfo.SourceCollection))
                    {
                        var targetSource = dropInfo.TargetCollection.Cast <object>().ToList();
                        var indexTarget  = targetSource.IndexOf(dropInfo.TargetItem);
                        var indexSource  = targetSource.IndexOf(dropInfo.DragInfo.SourceItem);

                        if (indexTarget == -1)
                        {
                            if (indexSource == dropInfo.InsertIndex - 1 && (dropInfo.InsertPosition & RelativeInsertPosition.TargetItemCenter) == 0)
                            {
                                dropInfo.Effects = DragDropEffects.None;
                            }
                        }


                        if (indexSource == indexTarget + 1 && dropInfo.InsertPosition == RelativeInsertPosition.AfterTargetItem && (dropInfo.InsertPosition & RelativeInsertPosition.TargetItemCenter) == 0)
                        {
                            dropInfo.Effects = DragDropEffects.None;
                        }
                        if (indexSource == indexTarget - 1 && dropInfo.InsertPosition == RelativeInsertPosition.BeforeTargetItem)
                        {
                            dropInfo.Effects = DragDropEffects.None;
                        }
                    }
                }
            }

            if (dropInfo.InsertPosition.HasFlag(RelativeInsertPosition.TargetItemCenter))
            {
                dropInfo.DropTargetAdorner = null;
            }
            else
            {
                dropInfo.DropTargetAdorner = dropInfo.Effects == DragDropEffects.None ? null : typeof(ToolboxInsertAdorner);
            }
        }