DiamonDogX

Hi. I have two ListBoxes, each using the same data template (the data template is defined so that the list box contains images). One listbox is bound to an ObservableCollection of images. I would like to be able to drag items from one listbox to another (or to another panel perhaps) so that the other listbox now contains the item I dropped onto it. I've looked at some samples... can't seem to even enable an item to be dragged FROM my first listbox. I also tried the Blendables DragAndDropListBox but couldn't get it to work...

Re: Windows Presentation Foundation (WPF) Drag and Drop between two similar listboxes?

Josh Smith

The demo project associated with this article shows how to drag-and-drop items between two ListViews, with the help of a utility class called ListViewDragDropManager. That might get you headed in the right direction. http://www.codeproject.com/WPF/ListViewDragDropManager.asp




Re: Windows Presentation Foundation (WPF) Drag and Drop between two similar listboxes?

vvatclor

Josh,

is your utility class applicable for doing a dragdrop between listbox of 2 different usercontrol

thanks




Re: Windows Presentation Foundation (WPF) Drag and Drop between two similar listboxes?

Josh Smith

It makes it easy for you to drag an item between two ListViews regardless of where they are.




Re: Windows Presentation Foundation (WPF) Drag and Drop between two similar listboxes?

vvatclor

Thanks for the response Josh!




Re: Windows Presentation Foundation (WPF) Drag and Drop between two similar listboxes?

DiamonDogX

OK I got the drag and dropping basically working. How would I make an adorner for the dragging element that is visible anywhere I drag the item in the application



Re: Windows Presentation Foundation (WPF) Drag and Drop between two similar listboxes?

Josh Smith

I don't know for sure, I've never tried it. I bet that you'll need to change the logic so that the adorner is added to the main Window's adorner layer. It is currently added to the adorner layer of the ListView, or ListViewItem (I forget exactly). You'll also need to get rid of the code which hides the adorner when the cursor leaves the ListView.




Re: Windows Presentation Foundation (WPF) Drag and Drop between two similar listboxes?

DiamonDogX

Yeah... I think that's probably right. I will try it and let you know! Thanks.



Re: Windows Presentation Foundation (WPF) Drag and Drop between two similar listboxes?

DiamonDogX

Josh, I am able to physically drag an item from my ListBox anywhere in my application, but what happens is (I am using your type of Adorner code), my adorner visual (while dragging) will not go outside my ListBox area. I still get the appropriate move icon when dragging to another listbox elsewhere in the application. It's just that the visual brush refuses to actually move outside the listbox area. Like you said, it has to do with the AdornerLayer and where it belongs in the UI and how the mouse coordinates are calculated. I suspect the problem is in the code below. Do you know what part of this code you would change so the adorner follows my mouse outside my listbox wherever it goes in my UI Btw, I have a singleton (MainWindow.Instance) as my top level application window. Feels like a simple fix, but can't seem to get it right.

Code Snippet

private AdornerLayer InitializeAdornerLayer( ListBoxItem itemToDrag )

{

// Create a brush which will paint the ListBoxItem onto

// a visual in the adorner layer.

VisualBrush brush = new VisualBrush( itemToDrag );

// Create an element which displays the source item while it is dragged.

this.dragAdorner = new ImageDragAdorner( this.sourceDragListBox, itemToDrag.RenderSize, brush );

// Set the drag adorner's opacity.

this.dragAdorner.Opacity = this.DragAdornerOpacity;

AdornerLayer layer = AdornerLayer.GetAdornerLayer( this.sourceDragListBox );

layer.Add( dragAdorner );

// Save the location of the cursor when the left mouse button was pressed.

this.ptMouseDown = MouseUtilities.GetMousePosition( this.sourceDragListBox );

return layer;

}

private void UpdateDragAdornerLocation()

{

if( this.dragAdorner != null )

{

Point ptCursor = MouseUtilities.GetMousePosition( this.sourceDragListBox );

double left = ptCursor.X;

ListBoxItem itemBeingDragged = this.GetListBoxItem( this.indexToSelect );

Point itemLoc = itemBeingDragged.TranslatePoint( new Point( 0, 0 ), this.sourceDragListBox );

double top = itemLoc.Y + ptCursor.Y - this.ptMouseDown.Y;

this.dragAdorner.SetOffsets( left, top );

}

}