New to Telerik UI for WPF? Download free 30-day trial

Legacy Drag Drop Mode

The RadTreeView's drag drop feature works with the Telerik DragDropManager, but originally it was implemented with the legacy RadDragAndDropManager.

DragDropManager is the newer implementation of RadDragAndDropManager used across the Telerik controls. In RadTreeView the legacy drag/drop customization entry points are still available for backwards compatibility. However, we recommend using the new DragDropManager implementation (available since Q2 2013).

The default drag drop execution mode is using the new DragDropManager (since Q2 2014), but this can be changed by setting the TreeViewSettings.DragDropExecutionMode property to Legacy. This is useful in case you upgrade from a very old version of Telerik UI for WPF to a more recent one and you have heavy customization on the drag/drop functionality using the drag/drop events of RadTreeView.

Example 1: Setting DragDropExecutionMode

<telerik:RadTreeView treeView:TreeViewSettings.DragDropExecutionMode="Legacy" /> 

The treeView namespace points to xmlns:treeView="clr-namespace:Telerik.Windows.Controls.TreeView;assembly=Telerik.Windows.Controls.Navigation"

The main difference between the DragDropManager and RadDragAndDropManager in the context of RadTreeView is that they provide different events for customization of the drag and drop actions. Read more in the DragDropManager Migration article.

The legacy mode works with the following set of RadTreeView drag/drop events which are propagated to the drag/drop events of RadDragAndDropManager:

  • PreviewDragStarted
  • DragStarted
  • PreviewDragEnded
  • DragEnded

Note that the legacy drag/drop events are not invoked in case you use the default DragDropExecutionMode which is called New.

RadTreeView actually handles the drag/drop events of RadDragAndDropManager which means that in order to subscribe new event handlers, you will need to use the AddHandler method of the control.

public void AddHandler( RoutedEvent routedEvent, Delegate handler, bool handledEventsToo );

When RadTreeView detects a valid drag operation, it invokes the PreviewDragStarted and DragStarted events. When a valid drop operation (the selected Item is dropped onto another Item or in between Items) is detected, the PreviewDragEnded and DragEnded events are invoked. Both PreviewDragStarted and PreviewDragEnded events can be cancelled by setting the Handled property of the event argument to True in the event handler.

Example 2: Cancel drag operation

private void radTreeView_PreviewDragEnded( object sender, RadTreeViewDragEndedEventArgs e ) 
{ 
    e.Handled = true; 
} 
Private Sub radTreeView_PreviewDragEnded(ByVal sender As Object, ByVal e As RadTreeViewDragEndedEventArgs) 
    e.Handled = True 
End Sub 

Handling the PreviewDragStarted event will cancel the drag operation. This is equivalent to set the RadTreeView's IsDragDropEnabled property to False. 'e.Handled = True' - where 'e' is the RadTreeViewDragEventArgs class passed as an argument of the event handler.

Handling the PreviewDragEnded event will cancel the drop operation. This is useful, when you want to cancel adding/removing items from the RadTreeView's ItemsCollection. 'e.Handled = True' - where 'e' is the RadTreeViewDragEventArgs class passed as an argument of the event handler.

The type of the event arguments for the PreviewDragStarted and DragStarted events is RadTreeViewDragEventArgs. Via the RadTreeViewDragEventArgs you can get access to the items being dragged:

Example 3: Get current dragged items

private void radTreeView_DragStarted( object sender, RadTreeViewDragEventArgs e ) 
{ 
    Collection<Object> draggedItems = e.DraggedItems; 
} 
Private Sub radTreeView_DragStarted(ByVal sender As Object, ByVal e As RadTreeViewDragEventArgs) 
    Dim draggedItems As Collection(Of [Object]) = e.DraggedItems 
End Sub 

The type of the event arguments for the PreviewDragEnded and DragEnded events is RadTreeViewDragEndedEventArgs. Via the RadTreeViewDragEndedEventArgs you can get access to the following items and properties:

  • DraggedItems: A collection of Items being dragged (this is useful when multi-selection is enabled - SelectionMode property of the RadTreeView is set to True). SelectionMode is enumeration.
  • DropPosition: Indicates the relationship of the Items being dropped and can be a DropPosition enumeration value After, Below or Inside.
  • TargetDropItem: The Item being dragged to.
  • IsCanceled: A boolean property, indicates whether the drag and drop operation is cancelled or not.

Example 4: Subscribing to the DragEnded event

private void radTreeView_DragEnded( object sender, RadTreeViewDragEndedEventArgs e ) 
{ 
    // Get the dragged items. 
    Collection<Object> draggedItems = e.DraggedItems; 
    // Get the drop position. 
    DropPosition dropPosition = e.DropPosition; 
    switch ( dropPosition ) 
    { 
        case DropPosition.After: 
            MessageBox.Show( "After" ); 
            break; 
        case DropPosition.Before: 
            MessageBox.Show( "Before" ); 
            break; 
        case DropPosition.Inside: 
            MessageBox.Show( "Inside" ); 
            break; 
    } 
    // Get is canceled 
    bool isCanceled = e.IsCanceled; 
    // Target drop item 
    RadTreeViewItem targetDropItem = e.TargetDropItem; 
    if ( targetDropItem.Header.ToString() == "Tennis" ) 
    { 
        // Do something 
    } 
} 
Private Sub radTreeView_DragEnded(ByVal sender As Object, ByVal e As RadTreeViewDragEndedEventArgs) 
    ' Get the dragged items. ' 
    Dim draggedItems As Collection(Of [Object]) = e.DraggedItems 
 
    ' Get the drop position. ' 
    Dim dropPosition__1 As DropPosition = e.DropPosition 
    Select Case dropPosition__1 
        Case DropPosition.After 
            MessageBox.Show("After") 
            Exit Select 
        Case DropPosition.Before 
            MessageBox.Show("Before") 
            Exit Select 
        Case DropPosition.Inside 
            MessageBox.Show("Inside") 
            Exit Select 
    End Select 
 
    ' Get is canceled ' 
    Dim isCanceled As Boolean = e.IsCanceled 
 
    ' Target drop item ' 
    Dim targetDropItem As RadTreeViewItem = e.TargetDropItem 
    If targetDropItem.Header.ToString() = "Tennis" Then 
        ' Do something ' 
    End If 
End Sub 

Note that the TargetDropItem property may be null if the drop is in an empty treeview. That's why when you use that property it always has to be checked:

Example 5: Checking TargetDropItem property

private void radTreeView_DragEnded(object sender, RadTreeViewDragEndedEventArgs e) 
{ 
  // Target drop item 
  RadTreeViewItem targetDropItem = e.TargetDropItem; 
  if (targetDropItem != null && targetDropItem.Header.ToString() == "Tennis" ) 
  { 
    // Do something 
  } 
}    

See Also

In this article