Drag-Drop from RadTreeView to custom control

2 posts, 0 answers
  1. PLuS
    PLuS avatar
    1 posts
    Member since:
    May 2015

    Posted 04 May 2015 Link to this post

    Hi,

    I have a RadTreeView with some items inside and want to drag and drop items to a custom control derived from RadControl (which is inside a DocumentWindow in a RadDock).

    I tried to attach TreeViewElement.DragDropService.PreviewDragOver event but it does not fire for my custom control. (Have set AllowDrop to true and tried to implement ISupportDrop interface but no luck)

    Any help would be appreciated.

  2. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 06 May 2015 Link to this post

    Hello Omid,

    Thank you for writing.

    RadTreeView controls the drag and drop functionality by TreeViewDragDropService. The RadTreeView.AllowDragDrop property should be set to true along with the drop target's AllowDrop property. If the custom control is a derivative of RadControl, the TreeViewDragDropService will search for an RadElement which is ISupportDrop. However, the RadControl is a basic class that does not have such an element. You can add a LightVisualElement or derive from another control, e.g. RadPanel.
    CustomControl c = new CustomControl();
     
    public Form1()
    {
        InitializeComponent();
     
        c.BackColor = Color.Red;
        this.splitPanel2.Controls.Add(c);
        c.Size = new System.Drawing.Size(100, 100);
     
        this.radTreeView1.AllowDragDrop = true;
        this.radTreeView1.TreeViewElement.DragDropService.PreviewDragOver += DragDropService_PreviewDragOver;
        this.radTreeView1.TreeViewElement.DragDropService.PreviewDragDrop += DragDropService_PreviewDragDrop;
    }
     
    private void DragDropService_PreviewDragDrop(object sender, RadDropEventArgs e)
    {
        LightVisualElement lve = e.HitTarget as LightVisualElement;
        if (lve == null)
        {
            return;
        }
        CustomControl c = lve.ElementTree.Control as CustomControl;
        if (c != null)
        {
            c.BackColor = Color.Yellow;
        }
    }
     
    private void DragDropService_PreviewDragOver(object sender, Telerik.WinControls.RadDragOverEventArgs e)
    {
        e.CanDrop = false;
        if (!(e.HitTarget is TreeNodeElement || e.HitTarget is RadTreeViewElement))
        {
            e.CanDrop = true;
        }
    }
     
    public class CustomControl : RadControl
    {
        protected override void CreateChildItems(RadElement parent)
        {
            base.CreateChildItems(parent);
            LightVisualElement lve = new LightVisualElement();
            lve.AllowDrop = true;
            parent.Children.Add(lve);
        }
    }

    I hope this information helps. Should you have further questions, I would be glad to help.
     
    Regards,
    Dess
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top