Cancel Drop
This article demonstrates how to cancel the drop operation. This is useful when you want to forbid the dropping on or the dropping of a particular item when using the RadTreeView control.
To do so, you can subscribe the RadTreeView element to the DragDropManager.Drop event and set the DropAction of the TreeViewDragDropOptions to DropAction.None.
Example 1: Subscribing the control for the Drop event
DragDropManager.AddDropHandler(this.radTreeView, OnDrop, true);
Example 2: Canceling the drag
private void OnDrop(object sender, Telerik.Windows.DragDrop.DragEventArgs e)
{
TreeViewDragDropOptions options = DragDropPayloadManager.GetDataFromObject(e.Data, TreeViewDragDropOptions.Key) as TreeViewDragDropOptions;
if (options != null && options.DragSourceItem != null)
{
// cancel the drop operation under a certain condition
options.DropAction = DropAction.None;
}
}
Code Example
This section contains a runnable code example showing how to cancel dropping on 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.AddDropHandler(this.radTreeView, OnDrop, true);
Example 5: Defining the DragInitialize handler and implementing logic that checks if the item can be dragged
private void OnDrop(object sender, Telerik.Windows.DragDrop.DragEventArgs e)
{
TreeViewDragDropOptions options = DragDropPayloadManager.GetDataFromObject(e.Data, TreeViewDragDropOptions.Key) as TreeViewDragDropOptions;
if (options != null && options.DragSourceItem != null)
{
RadTreeViewItem dropTargetItem = options.DropTargetItem;
if (dropTargetItem.Header.ToString() == "Football")
{
options.DropAction = DropAction.None;
}
}
}