New to Telerik UI for WinFormsStart a free 30-day trial

Drag and Drop

Updated over 6 months ago

RadPanorama handles the whole drag and drop operation by its TileDragDropService. The OnPreviewDragOver method allows you to control on what targets the tile being dragged can be dropped on. The OnPreviewDragDrop method allows you to get a handle on all the aspects of the drag and drop operation, the source (drag) RadPanorama, the destination (target) control, as well as the tile being dragged. This is where the actual physical move of the tile(s) from RadPanorama to the target control is performed.

Drag and Drop functionality between two RadPanorama controls

By default, RadPanorama supports drag and drop functionality of the tiles within the same RadPanorama. The following example demonstrates a sample approach how to handle the aforementioned events and achieve drag and drop behavior between two RadPanorama controls. There is a TileGroupElement added to each RadPanorama.

WinForms RadPanorama Drag Drop

C#
public class CustomTileDragDropService : TileDragDropService
{
    public CustomTileDragDropService(RadPanoramaElement owner) : base(owner)
    {
    }
 
    RadPanoramaElement draggedParent;
    RadPanoramaElement targetParent;
 
    protected override void OnPreviewDragOver(RadDragOverEventArgs e)
    {
        RadTileElement draggedTile = e.DragInstance as RadTileElement;
        if (draggedTile != null)
        {
            draggedParent = GetTileParent(draggedTile);               
            if (e.HitTarget is RadTileElement)
            {
                targetParent = GetTileParent(e.HitTarget as RadTileElement);
                if (targetParent != draggedParent)
                {
                    e.CanDrop = true;
                }
                else
                {
                    e.CanDrop = false;
                }
            }
            else
            {
                e.CanDrop = false;
            }
        }
    }
 
    private RadPanoramaElement GetTileParent(RadTileElement tile)
    {
        RadElement current = tile.Parent;
        while (current != null)
        {
            if (current is RadPanoramaElement)
            {
                current = current as RadPanoramaElement;
                break;
            }
            current = current.Parent;
        }
        return current as RadPanoramaElement ;
    }
 
    protected override void OnPreviewDragDrop(RadDropEventArgs e)
    {
        e.Handled = true;
        RadTileElement source = this.Context as RadTileElement;
        RadTileElement target = e.HitTarget as RadTileElement;
        TileGroupElement targetGroup = null;
        int insertIndex = 0;
        if (source == null)
        {
            return;
        }
        draggedParent.Items.Remove(source);
       
        if (target != null)
        {
            //groups are available
            if (targetParent.Groups.Count > 0)
            {
                targetGroup = target.Parent.Parent as TileGroupElement;
                insertIndex = targetGroup.Items.IndexOf(target);
            }
            else
            {
                insertIndex = targetParent.Items.IndexOf(target);
            }
            if (insertIndex > -1)
            {
                source.Column = target.Column;
                source.Row = target.Row;
                if (targetGroup != null)
                {
                    targetGroup.Items.Insert(insertIndex, source);
                }
                else
                {
                    targetParent.Items.Insert(insertIndex, source);
                }
            }
        }
        else
        {
            targetParent.Items.Add(source);
        }
 
        if (targetGroup != null)
        {
            AdjustPosition(source, insertIndex, targetGroup.Items);
        }
        else
        {
            AdjustPosition(source, insertIndex, targetParent.Items);
        }
    }
 
    private void AdjustPosition(RadTileElement source, int insertIndex, RadItemOwnerCollection items)
    {
        for (int i = insertIndex + 1; i < items.Count; i++)
        {
            RadTileElement tile = items[i] as RadTileElement;
            if (tile.Row == source.Row && tile.Column >= source.Column && tile != source)
            {
                tile.Column++;
            }
        }
    }
}

In order to replace the default TileDragDropService with the default one, it is necessary to set the PanoramaElement.DragDropService property:

C#
radPanorama1.PanoramaElement.DragDropService = new CustomTileDragDropService(radPanorama1.PanoramaElement);

See Also