Edit this page

Cancel Drag Start

This article demonstrates how to cancel the drag operation before it started. This is useful when you want to forbid dragging specific items from the treeview control.

To cancel the dragging you can subscribe the RadTreeView element to the DragDropManager.DragInitialize event and set the Data and DragVisual of its event arguments to null.

Example 1: Subscribing the control for DragInitialize

DragDropManager.AddDragInitializeHandler(this.radTreeView, OnTreeViewDragInitialize, true);

Example 2: Canceling the drag

private void OnTreeViewDragInitialize(object sender, DragInitializeEventArgs e)
{   
    // If the drag should be canceled set the data and the visual to null
    e.Data = null;
    e.DragVisual = null;
}

Code Example

This section contains a runnable code example showing how to cancel dragging for a specific item.

Example 3: Treeview definition

<telerik:RadTreeView x:Name="radTreeView" IsDragDropEnabled="True">
    <telerik:RadTreeViewItem Header="Sport Categories" IsExpanded="True">
        <telerik:RadTreeViewItem Header="Football" IsExpanded="True">
            <telerik:RadTreeViewItem Header="Futsal"/>
            <telerik:RadTreeViewItem Header="Soccer"/>
        </telerik:RadTreeViewItem>
        <telerik:RadTreeViewItem Header="Tennis"/>
        <telerik:RadTreeViewItem Header="Cycling"/>
    </telerik:RadTreeViewItem>
</telerik:RadTreeView>

Example 4: Subscribing the control for DragInitialize

DragDropManager.AddDragInitializeHandler(this.radTreeView, OnTreeViewDragInitialize, true);

Example 5: Defining the DragInitialize handler and implementing logic that checks if the item can be dragged

private void OnTreeViewDragInitialize(object sender, DragInitializeEventArgs e)
{
    TreeViewDragDropOptions options = DragDropPayloadManager.GetDataFromObject(e.Data, TreeViewDragDropOptions.Key) as TreeViewDragDropOptions;
    if (options != null && options.DragSourceItem != null)
    {
        RadTreeViewItem draggedItem = options.DragSourceItem;
        if (draggedItem.Header.Equals("Sport Categories"))
        {
            e.Data = null;
            e.DragVisual = null;
        }
    }
}

In this case you cannot drag the "Sport Categories" item which is the root of the treeview.

You can find a runnable example demonstrating this approach in a data binding scenario in our GitHub SDK repository after navigating to TreeView/CancelDragStart.

See Also