protected override void OnDrop(DragEventArgs e)
        {
            ModelTreeManager manager = this.Context.Services.GetService <ModelTreeManager>();

            // When dragging from toolbox:
            //     editingScope should not be null
            //     there should only be one item
            // When dragging from canvas:
            //     editingScope should be null
            // Call editingScope.Complete() to commit changes, otherwise the editing scope will be aborted
            using (EditingScope editingScope = ModelItemHelper.TryCreateImmediateEditingScope(manager, SR.PropertyChangeEditingScopeDescription))
            {
                List <object> droppedObjects = this.GetSortedObjectList(e);
#pragma warning disable 618
                DragDropHelper.SetDragDropCompletedEffects(e, DragDropEffects.None);
#pragma warning restore 618
                if (droppedObjects == null || droppedObjects.Count == 0)
                {
                    return;
                }
                if (droppedObjects.Count == 1)
                {
                    if (this.DoSingleDrop(droppedObjects[0], e))
                    {
                        if (editingScope != null)
                        {
                            editingScope.Complete();
                        }
                    }
                    return;
                }
                else
                {
                    // multi drop
                    Fx.Assert(editingScope == null, "editingScope should be null for dragging from canvas.");
                    this.DoAutoWrapDrop(InsertionPosition.None, e, droppedObjects);
                }
                base.OnDrop(e);
            }
        }
            void OnPlaceholderDrop(object sender, DragEventArgs e)
            {
                if (!this.AllowDropOnSpacer(e))
                {
                    return;
                }
                InsertionPosition insertionPos = (sender == this.BottomSpacerWrapper.Placeholder)
                    ? InsertionPosition.After : InsertionPosition.Before;

                ModelItemHelper.TryCreateImmediateEditingScopeAndExecute(this.wfItemPresenter.Context, SR.WrapInSequenceDescription, (es) =>
                {
                    if (this.wfItemPresenter.DoAutoWrapDrop(insertionPos, e))
                    {
                        // auto wrap is successful
                        if (es != null)
                        {
                            // if we created an immediate editing scope, try to complete it.
                            es.Complete();
                        }
                    }
                });
            }
        void OnItemsDropped(DragEventArgs e, int index)
        {
            ModelItemHelper.TryCreateImmediateEditingScopeAndExecute(this.Items.GetEditingContext(), System.Activities.Presentation.SR.CollectionAddEditingScopeDescription, (es) =>
            {
                DragDropHelper.SetDragDropCompletedEffects(e, DragDropEffects.None);
                List <object> droppedObjects = new List <object>(DragDropHelper.GetDroppedObjects(this, e, Context));
                List <WorkflowViewElement> movedViewElements = new List <WorkflowViewElement>();

                List <object> externalMoveList    = new List <object>();
                List <ModelItem> internalMoveList = new List <ModelItem>();

                // Step 1: Sort the list
                List <object> sortedDroppingList = DragDropHelper.SortSelectedObjects(droppedObjects);


                // Step 2: Categorize dropped objects by their source container.
                foreach (object droppedObject in sortedDroppingList)
                {
                    ModelItem modelItem      = droppedObject as ModelItem;
                    WorkflowViewElement view = (modelItem == null) ? null : (modelItem.View as WorkflowViewElement);
                    if (view == null)
                    {
                        externalMoveList.Add(droppedObject);
                        continue;
                    }
                    UIElement container = DragDropHelper.GetCompositeView(view);
                    if (container == this)
                    {
                        internalMoveList.Add(modelItem);
                        continue;
                    }
                    movedViewElements.Add(view);
                    externalMoveList.Add(droppedObject);
                }

                // Step 3: Internal movement
                if (this.ShouldMoveItems(internalMoveList, index))
                {
                    foreach (ModelItem modelItem in internalMoveList)
                    {
                        int oldIndex = this.Items.IndexOf(modelItem);
                        this.Items.Remove(modelItem);

                        //if element is placed ahead of old location, decrement the index not to include moved object
                        if (oldIndex < index)
                        {
                            this.InsertItem(index - 1, modelItem);
                        }
                        else
                        {
                            this.InsertItem(index, modelItem);
                            index++;
                        }
                    }
                }

                // Step 4: External move and drop from toolbox
                foreach (object droppedObject in externalMoveList)
                {
                    if (!this.IsDropAllowed(droppedObject))
                    {
                        continue;
                    }
                    this.InsertItem(index++, droppedObject);
                    DragDropHelper.SetDragDropCompletedEffects(e, DragDropEffects.Move);
                }
                DragDropHelper.SetDragDropMovedViewElements(e, movedViewElements);
                e.Handled = true;
                if (es != null)
                {
                    es.Complete();
                }
            });
        }
        bool DoAutoWrapDrop(InsertionPosition insertionPos, DragEventArgs e, IList <object> droppedObjects = null)
        {
            if (droppedObjects == null)
            {
                ModelTreeManager manager      = this.Context.Services.GetRequiredService <ModelTreeManager>();
                EditingScope     editingScope = null;

                try
                {
                    editingScope = ModelItemHelper.TryCreateImmediateEditingScope(manager, SR.WrapInSequenceDescription);

                    droppedObjects = this.GetSortedObjectList(e);

                    if (!this.DoAutoWrapDrop(insertionPos, droppedObjects))
                    {
                        return(false);
                    }

                    if (editingScope != null)
                    {
                        editingScope.Complete();
                    }
                }
                finally
                {
                    if (editingScope != null)
                    {
                        editingScope.Dispose();
                        editingScope = null;
                    }
                }
            }
            else
            {
                if (!this.DoAutoWrapDrop(insertionPos, droppedObjects))
                {
                    return(false);
                }
            }

            if (!DragDropHelper.IsDraggingFromToolbox(e))
            {
                List <WorkflowViewElement> movedViewElements = ObjectList2WorkflowViewElementList(droppedObjects);
                DragDropHelper.SetDragDropMovedViewElements(e, movedViewElements);

                //Backward compatibility for 4.0
                if (droppedObjects.Count == 1 && movedViewElements.Count == 1)
                {
                    #pragma warning disable 618
                    DragDropHelper.SetDragDropCompletedEffects(e, DragDropEffects.Move);
                    #pragma warning restore 618
                }
            }
            else
            {
                Fx.Assert(droppedObjects.Count == 1, "Dropping from Toolbox with count != 1");

                // Set focus if it is dropping from ToolBox.
                // In common drag/drop, the selection setting is done at the end of
                // StartDragging().
                if (this.Item == null)
                {
                    return(true);
                }

                Fx.Assert(typeof(Sequence).IsAssignableFrom(this.Item.ItemType),
                          "Auto Wrap didn't add a sequence. Is Item.Properties[\"Activities\"] still correct?");
                foreach (ModelItem item in this.Item.Properties["Activities"].Collection)
                {
                    // Find the ModelItem whose value is an activity from Toolbox.
                    if (item.GetCurrentValue() == droppedObjects[0])
                    {
                        this.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, (Action)(() =>
                        {
                            item.Focus();
                        }));
                        break;
                    }
                }
            }

            return(true);
        }