Skip to main content Skip to footer

Drag and Drop Multiple Items in C1TreeView

ComponentOne TreeView supports drag-and-drop operations within the tree. We just need to set the 'AllowDragDrop' property to 'True' and the users will be able to reorder nodes within the tree by dragging them with the mouse. C1TreeView trigger events during drag-drop operations such that these operations can be customized as per the user requirement. We do have a C1TreeViewDragDrop sample (both in WPF and Silverlight) wherein a 'CustomDragDrop' class is defined. This class is used to customize the drag drop operations by validating the drag drop operations and allowing move and copy actions. In this blog, we will extend the functionality provided in the sample, that is, support for Dragging and Dropping MULTIPLE C1TreeViewItem(s) within the C1TreeView.

Support for selecting Multiple Items in C1TreeView

Set the 'SelectionMode' property of the C1TreeView to 'Extended' either in code or modify the Xaml marup as :



<C1:C1TreeView x:Name="SampleTreeView" Grid.Row="1" Grid.ColumnSpan="2" BorderBrush="#FF8FB4CC" ItemTemplateSelector="{StaticResource CustomTemplateSelector}" Padding="10" SelectionMode="Extended"/>  


Select Multiple Items for Drag and Drop Operations

In the HandleDragDrop method, generate a list of all the Items currently selected in the C1TreeView and pass these Items to the DoMove and DoCopy methods respectively.


public void HandleDragDrop(object source, DragDropEventArgs e)  
{  
if (e.Effect != DragDropEffect.None)  
{  
// make the move...  
C1TreeViewItem item = e.DragSource as C1TreeViewItem;  
if (item != null)  
{  
    // find target node  
    C1TreeViewItem target = Tree.GetNode(e.GetPosition(null));  
    if (target != null)  
    {  
        Point p = e.GetPosition(target);  
        bool insertAfter = (p.Y > target.RenderSize.Height / 2);  

        // move/copy all the selected items to target.  
        IList tempItems = new List();  
        foreach (C1TreeViewItem storeItem in Tree.SelectedItems)  
        {  
            tempItems.Add(storeItem);  
        }  

        if (Effect == DragDropEffect.Move)  
        {  
            foreach (C1TreeViewItem moveItem in tempItems)  
            {  
                DoMove(moveItem, target, insertAfter);  
            }  
        }  
        else  
        {  
            foreach (C1TreeViewItem moveItem in tempItems)  
            {  
                DoCopy(moveItem, target, insertAfter);  
            }  
        }  
    }  
}  
}  
}  

C1TreeViewDragDrop Download Sample SL Download Sample WPF

MESCIUS inc.

comments powered by Disqus