Edit this page

ListBoxDragDropBehavior

In this article you can find information about the ListBoxDragDropBehavior class.

Attaching the behavior

  • Add a reference to Telerik.Windows.Controls.dll and add the following XML namespaces to your XAML:

  • Create a ListBoxItem Style that will enable the drag of the ListBoxItem controls:

<Style x:Key="DraggableListBoxItem" TargetType="ListBoxItem">
    <Setter Property="telerik:DragDropManager.AllowCapturedDrag" Value="True" />
</Style>
  • Attach the ListBoxDragDropBehavior behavior:

<ListBox ItemContainerStyle="{StaticResource DraggableListBoxItem}">
    <drag:ListBoxDragDrop.Behavior>
        <drag:ListBoxDragDropBehavior />
    </drag:ListBoxDragDrop.Behavior>
</ListBox>

Features

The ListBoxDragDropBehavior provides drag-drop capabilities for standard ListBox controls. It automatically recognizes its host control item Type and automatically adds the dragged items that are of this type. In some cases the ListBoxDragDropBehavior will be unable to correctly recognize its host’s item Type. The ItemType property is provided for such cases (Silverlight 5 example):

<drag:ListBoxDragDropBehavior ItemType="{x:Type local:IBaseClass}" />

In Silverlight 4 you will have to either inherit from ListBoxDragDropBehavior and set the ItemType property in the class constructor, or bind it to a property in your view model that contains the correct Type.

By default the ListBoxDragDropBehavior will allow reordering/inserting items in the ListBox. To disable this feature, set AllowReorder=false on the behavior:

<drag:ListBoxDragDropBehavior AllowReorder="false" />

The ListBoxDragDropBehavior supports copying the dragged items if the Control key is pressed. For that purpose the CopyDraggedItems method should be overridden:

protected override IEnumerable<object> CopyDraggedItems(ListBoxDragDropState state)
{
    return state.DraggedItems.OfType<Customer>().Select(c => c.Copy()).Cast<object>();
} 

Converting data

In scenarios with drag-drop between controls containing different item types the drag data should be converted using a DataConverter.

  • Create a new class, deriving from DataConverter:

public class CustomerToAppointmentConverter : DataConverter
{
}
  • Override the GetConvertToFormats() and ConvertTo(). The following method can convert from Customer to Appointment and vice versa:

public override string[] GetConvertToFormats()
{
    return new string[] { typeof(Appointment).FullName, typeof(Customer).FullName };
}
public override object ConvertTo(object data, string format)
{
    var customers = (IEnumerable)DataObjectHelper.GetData(data, typeof(Customer), false);
    if (customers != null && format == typeof(Appointment).FullName)
    {
        return customers.OfType<Customer>().Select(c => new Appointment { Subject = c.Name });
    }
    var appointments = (IEnumerable)DataObjectHelper.GetData(data, typeof(Appointment), false);
    if (appointments != null && format == typeof(Customer).FullName)
    {
        return appointments.OfType<Appointment>().Select(a => new Customer { Name = a.Subject });   
    }
    return null;
}   
  • Attach the newly created DataConverter:

<ListBox ...>
    <drag:ListBoxDragDrop.DataConverter>
        <local:CustomerToAppointmentConverter />
    </drag:ListBoxDragDrop.DataConverter>
    <drag:ListBoxDragDrop.Behavior>
        <drag:ListBoxDragDropBehavior />
    </drag:ListBoxDragDrop.Behavior>
</ListBox>
Was this article helpful? Yes No

Give article feedback

Tell us how we can improve this article

Dummy