Skip to content

Drag and Drop and Instancing

Gary edited this page Aug 27, 2014 · 1 revision

Table of Contents

You also use instancing with drag and drop editing, as well as with editing using menu items. Drag events are in the context of a control, so you define drag event handlers for the control.

Handling Dragging Events

Create handlers for the DragOver and DragDrop events on the control in which items can be dropped. DragOver occurs when the the cursor is dragged over the control. DragDrop occurs when the mouse is released over the control after dragging.

This handling is well illustrated in the ATF Simple DOM Editor Sample, from which the subsequent code examples are taken. This editor creates lists of sequences. Sequences can have resources added to them by dragging a resource icon from a palette onto the Resources window's ListView control. The ResourceListEditor component creates this ListView control. This component also defines handlers on the ListView for the dragging events in its IInitializable.Initialize() method. For other programming details of instance handling in that sample, see IInstancingContext Interface.

DragOver Event

The drag over event indicates whether an insertion is possible:

private void resourcesListView_DragOver(object sender, DragEventArgs e)
{
    if (m_event != null)
    {
        EventContext context = m_event.Cast<EventContext>();
        e.Effect = DragDropEffects.None;
        if (context.CanInsert(e.Data))
        {
            e.Effect = DragDropEffects.Move;
            ((Control)sender).Focus(); // Focus the list view; this will cause its context to become active
        }
    }
}

The event argument is an instance of the EventContext class, which implements IInstancingContext. If IInstancingContext.CanInsert() indicates that insertion is possible, the cursor changes to show where the resource can be dropped. The ListView control is also activated.

DragDrop Event

The drag over event performs the resource insertion:

private void resourcesListView_DragDrop(object sender, DragEventArgs e)
{
    if (m_event != null)
    {
        IInstancingContext context = m_event.Cast<IInstancingContext>();
        if (context.CanInsert(e.Data))
        {
            ITransactionContext transactionContext = context as ITransactionContext;
            TransactionContexts.DoTransaction(transactionContext,
                delegate
                {
                    context.Insert(e.Data);
                },
                Localizer.Localize("Drag and Drop"));

            if (m_statusService != null)
                m_statusService.ShowStatus(Localizer.Localize("Drag and Drop"));
        }
    }
}

Similarly to the drag over event, this handler first checks that the insertion is possible. It adapts the event parameter to both an IInstancingContext and an ITransactionContext. EventContext derives from EditingContext, which ultimately implements ITransactionContext in one of its derived classes, TransactionContext. Similarly to the paste operation from a menu item, the insertion is done by calling IInstancingContext.Insert() inside a transaction using an ITransactionContext object.

Topics in this section

Clone this wiki locally