# DragDropManager Migration

The article explains the events of the **DragDropManager**.

## Events and API migration

The event that was fired when the drag Starts was **DragQuery** for the RadDragAndDropManager. Now it is **OnDragInitialize**.

Below is the code for the DragQuery event and its corresponding code for the OnDragInitialize event:

```C#
private void OnDragQuery(object sender, DragDropQueryEventArgs e)
{
    ListBoxItem listBoxItem = e.Options.Source as ListBoxItem;
    System.Windows.Controls.ListBox box = ItemsControl.ItemsControlFromItemContainer(listBoxItem) as System.Windows.Controls.ListBox;
    if (e.Options.Status == DragStatus.DragQuery && box != null)
    {
        e.Options.Payload = box.SelectedItem;
        ContentControl cue = new ContentControl();
        cue.ContentTemplate = this.Resources["ApplicationDragTemplate"] as DataTemplate;
        cue.Content = box.SelectedItem;
        e.Options.DragCue = cue;
        e.Options.ArrowCue = RadDragAndDropManager.GenerateArrowCue();
    }
    e.QueryResult = true;
}
```

```C#
private void OnDragInitialize(object sender, DragInitializeEventArgs args)
{
    args.AllowedEffects = DragDropEffects.All; // Coresponds to the QueryResult

    // Coresponds to the payload setting
    var payload = DragDropPayloadManager.GeneratePayload(null);
    payload.SetData("DragData", ((FrameworkElement)args.OriginalSource).DataContext);
    args.Data = payload;

    // Coresponds to the cue setting (e.Options.DragCue)
    args.DragVisual = new ContentControl { Content = args.Data, ContentTemplate = LayoutRoot.Resources["ApplicationTemplate"] as DataTemplate };
}
```

The event corresponding to the **DragInfo** event in RadDragAndDropManager is now **DragDropComplete**. The **DragDropComplete** event is the equivalent of the **DragQuery with status DropComplete**.

```C#
private void OnDragInfo(object sender, DragDropEventArgs e)
{
    ListBoxItem listBoxItem = e.Options.Source as ListBoxItem;
    System.Windows.Controls.ListBox box = ItemsControl.ItemsControlFromItemContainer(listBoxItem) as System.Windows.Controls.ListBox;
    IList<ApplicationInfo> itemsSource = box.ItemsSource as IList<ApplicationInfo>;
    ApplicationInfo payload = e.Options.Payload as ApplicationInfo;
    if (e.Options.Status == DragStatus.DragComplete)
    {
        if (payload != null && itemsSource.Contains(payload))
        {
            itemsSource.Remove(payload);
        }
    }
}
```

```C#
public void OnDragCompleted(object sender, Telerik.Windows.DragDrop.DragDropCompletedEventArgs args)
{
    if (args.Effects != DragDropEffects.Scroll && args.Effects != DragDropEffects.Move)
    {
        var sourceControl = sender as System.Windows.Controls.ListBox;
        var sourceItems = sourceControl.ItemsSource as IList;
        var draggedData = DragDropPayloadManager.GetDataFromObject(args.Data, "DragData");

        if (sourceItems != null)
        {
            sourceItems.Remove(draggedData);
        }
    }
}
```

The **Drop** event in DragDropManager corresponds to the **DropInfo event with status DropCompleted**.

```C#
private void OnDropInfo(object sender, DragDropEventArgs e)
{
    ItemsControl box = e.Options.Destination as ItemsControl;
    IList<ApplicationInfo> itemsSource = box.ItemsSource as IList<ApplicationInfo>;
    ApplicationInfo payload = e.Options.Payload as ApplicationInfo;
    if (e.Options.Status == DragStatus.DropComplete)
        if (!itemsSource.Contains(payload))
            itemsSource.Add(payload);
}
```

```C#
private void OnDrop(object sender, Telerik.Windows.DragDrop.DragEventArgs args)
{
    var destinationControl = sender as System.Windows.Controls.ListBox;
    var destinationItems = destinationControl.ItemsSource as IList;
    var draggedData = DragDropPayloadManager.GetDataFromObject(args.Data, "DragData");

    if (draggedData != null && args.Effects != DragDropEffects.None)
    {
        destinationItems.Add(draggedData);
    }
}
```

The **DragInfo** event corresponds to the **GiveFeedback** event. Basically the GiveFeedback event can be used on the DragSource control to update the Cursor, and the DragEffects for the DragOperation.

The **DropInfo** event corresponds to the **DragOver** event of DragDropManager. The **DragOver** event can be used as a replacement for the **DropQuery/Info** events for various purposes, one of which is to **update the AllowedEffects** for the drag drop operation.  Works both on Source and Destination.

```C#
private void OnDragOver(object sender, Telerik.Windows.DragDrop.DragEventArgs e)
{
    if (!(e.OriginalSource is FrameworkElement))
    {
        e.Effects = DragDropEffects.None;
        e.Handled = true;
    }
}
```

The **CancelDrag** event corresponds to the **QueryContinueDrag** event. The CancelDrag() method logic can be replaced by subscribing to the QueryContinueDrag event and manipulating the Action property in the QueryContinueEventArgs.

The control cursor can be set in the GiveFeedbackEventArgs.

```C#
private void OnGiveFeedback(object sender, Telerik.Windows.DragDrop.GiveFeedbackEventArgs args)
{
    args.SetCursor(Cursors.Arrow);
    args.Handled = true;
}
```

## Events without analogue

* PreviewGiveFeedback

* PreviewQueryContinueDrag

* PreviewDragEnter

* PreviewDragLeave

* PreviewDragOver

* PreviewDrop

## Missing Functionality

* There is no AutoScrollBehavior out of the box

* There is no DragArrow Cue
