Multi-Item Drag and Drop RadListBox
Posted by: the telerik blogs,
on 27 Aug 2009 |
View original | Bookmarked: 0 time(s)
Earlier this week, I ran across a forum post asking about how one would go about creatingmultiple RadListBoxes that supportedmulti-item drag and drop. This sounded like a fun challenge, so I decided to take it on.Referencing an earlier forum post that described single-item drag and drop, I came up with the following solution.
Calling DoDragDrop() immediately changes the mouse pointer and readies the RadListBoxfor the drag-and-drop operation. Originally, I had tried calling this function in the MouseDown event, but this lead to unpredictable clunky behavior due to the fact that DoDragDrop() was getting called before I had the chance to select multiple items. In the following code, Ive set up the DoDragDrop() method to get called in the MouseMove event when the mouse button is currently down. I also made sure to keep track of in-progress drag operationsin order to preventcalling the DoDragDrop() methodhundred oftimes. (Note: Both RadListBoxes must be subscribed to these events)
| boolisMouseDown=false; |
| boolisDragAndDrop=false; |
| privatevoidradListBox_MouseDown(objectsender,MouseEventArgse) |
| { |
| isMouseDown=true; |
| } |
|
| privatevoidradListBox_MouseMove(objectsender,MouseEventArgse) |
| { |
| if(!isMouseDown||isDragAndDrop)return; |
|
| RadListBoxlistBox=senderasRadListBox; |
| listBox.DoDragDrop(listBox.SelectedItems.ToList(),DragDropEffects.Copy); |
|
| isDragAndDrop=true; |
| } |
|
| privatevoidradListBox_MouseUp(objectsender,MouseEventArgse) |
| { |
| isMouseDown=false; |
| isDragAndDrop=false; |
| } |
In the DragDrop event, I simply iterated through the list of RadItems I passed to the event through the DoDragDrop() method, swapping each item from its former RadListBox to the new RadListBox.
| privatevoidradListBox_DragOver(objectsender,DragEventArgse) |
| { |
| e.Effect=DragDropEffects.Copy; |
| } |
|
| privatevoidradListBox_DragDrop(objectsender,DragEventArgse) |
| { |
| RadListBoxlistBox=senderasRadListBox; |
|
| List<RadItem>items=(List<RadItem>)e.Data.GetData(typeof(List<RadItem>)); |
| RadListBoxsourceListBox=items[0].ElementTree.ControlasRadListBox; |
|
| foreach(RadItemlstBoxIteminitems) |
| { |
| sourceListBox.Items.Remove(lstBoxItem); |
| listBox.Items.Add(lstBoxItem); |
| } |
| } |
Try it out yourself, orclick here to download the source code!