This is a migrated thread and some comments may be shown as answers.

How to 'merge' dragged object and destination object with same ID and update its quantity?

3 Answers 57 Views
DragAndDrop
This is a migrated thread and some comments may be shown as answers.
Dexter
Top achievements
Rank 1
Dexter asked on 07 Nov 2014, 09:26 PM
Hi, I would like to enquire on a solution to the problem as per title. I am dragging between 2 RadListBox. Below is an example of the desired effect I would like to achieve:

A drag drop event

Dragged 'Equipment' objects (with ID and Quantity properties):
ID: 2, Quantity: 2
ID: 5, Quantity: 3
ID: 9, Quantity: 1

Destination Objects ('Equipment' objects already in destination RadListBox):
ID: 1, Quantity: 3
ID: 2, Quantity: 3
ID: 9, Quantity: 2


Desired outcome in destination RadListBox after drag drop event:
ID: 1, Quantity: 3
ID: 2, Quantity: 5 (3+2)
ID: 5, Quantity: 3
ID: 9, Quantity: 3 (2+1)

Instead of:
ID: 1, Quantity: 3
ID: 2, Quantity: 3
ID: 9, Quantity: 2
ID: 2, Quantity: 2
ID: 5, Quantity: 3
ID: 9, Quantity: 1


Thanks a lot for your attention. Please let me know if further details is needed.





3 Answers, 1 is accepted

Sort by
0
Kalin
Telerik team
answered on 12 Nov 2014, 11:26 AM
Hi Dexter,

You will be able to achieve the desired by using our DragDropManager - in the drop handler of the second ListBox you will need to check whether there is an item in its ItemsSource with the same ID. If so you will have change the quantity as needed or if there isn't such an item to manually add it. For more details on how to achieve such a drag drop functionality between two ListBoxes you can check the following article from our online help documentation:
http://www.telerik.com/help/wpf/dragdropmanager-dragdroppayloadmanager.html

The Drop handler should be implemented as demonstrated below:

private void OnDropHandler(object sender, Telerik.Windows.DragDrop.DragEventArgs e)
{
    var data = DragDropPayloadManager.GetDataFromObject(e.Data, "DragData") as Equipment;
    var destinationItemsSource = (sender as RadListBox).ItemsSource.OfType<Equipment>().ToList();
    var item = destinationItemsSource.FirstOrDefault(x => x.ID == data.ID);
 
    if (item != null)
    {
        item.Quantity += data.Quantity;
    }
    else
    {
        destinationItemsSource.Add(data);
    }
 
    e.Handled = true;
}

Please give it a try and if you have any further questions, let us know.

Hope this helps.

Regards,
Kalin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Dexter
Top achievements
Rank 1
answered on 13 Nov 2014, 07:33 AM
Hi Kalin, thanks for your attention, the example provided is great, but is it possible to do multi drag drop based on all selected items?

Also, how to implement a full ScreenshotDragVisualProvider in the example?
The code below I tried only provides the text visual of the dragged item, as that is its template. I have a custom listbox style and listboxitem style. How to include them in the visual such that it is a complete screenshot drag visual?
e.DragVisual = new ContentControl { ContentTemplate = ((RadListBox)sender).ItemTemplate, Content = data };

0
Accepted
Kalin
Telerik team
answered on 17 Nov 2014, 01:09 PM
Hi Dexter,

If you need to drag and display multiple ListBoxItems you would need to use ItemsControl as DragVisual - this way you will be able to set the same ItemTemplate of the ListBox as ItemTemplate of the ItemsControl. If the ItemTemplate is located in the MainWindow you can get in the code behind the following way:

this.Resources["ListBoxItemTemplate"] as DataTemplate

Please check the following code snippet that demonstrates the exact implementation of the explained above:

private void OnDragInitialize(object sender, DragInitializeEventArgs e)
{
    e.AllowedEffects = DragDropEffects.All;
    var payload = DragDropPayloadManager.GeneratePayload(null);
    var data = new List<Equipment>((sender as RadListBox).SelectedItems.Cast<Equipment>());
    payload.SetData("DragData", data);
    e.Data = payload;
    e.DragVisual = new ItemsControl { ItemsSource = data, ItemTemplate = this.Resources["ListBoxItemTemplate"] as DataTemplate };
    e.Handled = true;
}

Hope this helps.

Regards,
Kalin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
DragAndDrop
Asked by
Dexter
Top achievements
Rank 1
Answers by
Kalin
Telerik team
Dexter
Top achievements
Rank 1
Share this question
or