NTreeViewEx block item drop


https://www.nevron.com/Forum/Topic11979.aspx
Print Topic | Close Window

By Juan de Ituarte López - 5 Years Ago

In a NTreeViewEx control I want to be able to block dropping a node to other nodes.
I want to achieve the same result as the default implementation when it blocks me from dropping a node to itself or its parent, but to other nodes of my choice.
How can I achieve this?
Thanks

By Nevron Support - 5 Years Ago
Hello Juan,

To do this you will need to subclass NTreeViewEx and override ApplyDragDrop method as follows:


public class MyNTreeViewEx : NTreeViewEx
{
    protected override void ApplyDragDrop(NLightUIItemDragDropEventArgs e)
    {
        NTreeNode over = e.DragOverItem as NTreeNode;
        // condition which determines that this is the node(s) that cannot
        // drop another node on it.
        // In this case you can set Tag property of the node to true,
        // or you can choose another approach.
        if ((bool)over.Tag == true)
        {
            return;
        }

        base.ApplyDragDrop(e);
    }
}


Also you can attach the instance of the new class to ItemDrag event and in the event handler you can change the cursor when the user drag over the node that should not accept dropped node:


myNTreeViewEx1.ItemDrag += myNTreeViewEx1_ItemDrag;
...
void myNTreeViewEx1_ItemDrag(object sender, NLightUIItemDragDropEventArgs e)
{
    NTreeViewEx treeView = sender as NTreeViewEx;

    // get the hovered node
    NTreeNode node = treeView.VisibleNodeFromY(e.MousePosition.Y);
    if (node == null)
    {
        return;
    }

    Cursor cur = e.Cursor;

    if ((bool)node.Tag == true && e.Cursor == null)
    {
        e.Cursor = Cursors.No;    
    }
    else
    {
        e.Cursor = cur;
    } 
}


I hope this helps.
By Juan de Ituarte López - 5 Years Ago
Perfect answer, it works like a charm, thanks!!!!

To make the control generic I've subclassed NTreeViewEx and overrided ApplyDragDrop method as follow to add a new event ItemAllowDrag:

public class MyTreeViewEx : NTreeViewEx
{
    public delegate bool MyItemDragDropEventHandler(object sender, NLightUIItemDragDropEventArgs e);
    public event MyItemDragDropEventHandler ItemAllowDrag;

    protected override void ApplyDragDrop(NLightUIItemDragDropEventArgs e)
    {
        if (ItemAllowDrag != null)
        {
            if (!ItemAllowDrag(this, e))
                return;
        }
        base.ApplyDragDrop(e);
    }
}

And then I attached 2 events to my control:

private bool TreeList_ItemAllowDrag(object sender, NLightUIItemDragDropEventArgs e)
{
    if (e.Item.Name == "FOLDER")
        return false;
    if (e.DragOverItem.Name == "COMPONENT")
        return false;
    return true;
}

private void TreeList_ItemDrag(object sender, NLightUIItemDragDropEventArgs e)
{
    NLightUIItem DragOverItem = TreeList.ItemFromPoint(e.MousePosition);
    if (e.Item.Name != "COMPONENT")
        e.Cursor = Cursors.No;
    else if (DragOverItem != null && DragOverItem.Name == "COMPONENT")
        e.Cursor = Cursors.No;
    else
        e.Cursor = null;
}


Is it possible to also avoid the node hover effect?

Thanks,
Juan