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

The ListBoxDragDropBehavior's Drop Event Occurs When CanDrop Returns False

Environment

Product Version 2023.3.1114
Product RadListBox for WPF

Description

The Drop event handler of the RadListBox's default ListBoxDragDropBehavior can occur even if the CanDrop event returns false. This can be present in a scenario when moving fast with the mouse while dragging and dropping an element.

This behavior comes from the WPF framework. When dragging and dropping an element fast, the visual elements that are part of the RadListBoxItem's ControlTemplate invoke the Drop method. It is then bubbled to the RadListBox control.

Solution

Create a new class that derives from the ListBoxDragDropBehavior and override the Drop method. In it, retrieve the return value of the the CanDrop method invocation. If the return value is true, call the base Drop method.

Custom ListBoxDragDropBehavior with overridden Drop method

public class CustomListBoxDragDropBehavior : ListBoxDragDropBehavior 
{ 
    public override void Drop(DragDropState state) 
    { 
        if (this.CanDrop(state)) 
        { 
            base.Drop(state); 
        } 
    } 
} 
Public Class CustomListBoxDragDropBehavior 
    Inherits ListBoxDragDropBehavior 
 
    Public Overrides Sub Drop(ByVal state As DragDropState) 
        If Me.CanDrop(state) Then 
            MyBase.Drop(state) 
        End If 
    End Sub 
End Class 
In this article