4
votes

I have a WPF ListBox, where the items are styled to be Buttons. I've used gong-wpf-dragdrop to very easily implement drag-and-drop, so I can reorder the listitems. However, I would now like to be able to left-click on one of the buttons to launch an edit action, but the MouseLeftButtonUp event on the ListBox seems to be being swallowed by the drag-and-drop operation. (I'm using EventToCommand from MVVM Light to hook everything up).

Changing the ListBox to respond to MouseRightButtonUp works fine (so I can drag-and-drop with the left mouse button, and launch the edit action with the right mouse button), but I would rather keep the right mouse button for a context menu.

I also tried using MouseDoubleClick, but although that launched the edit action, it always opened the first item in the list for editing, and moved the listitem that was double-clicked to the top of the list - very confusing for everyone concerned!

Any thoughts on how to approach this?

1
The gong-wpf-dragdrop example doesn't initiate the DoDragDrop until the mouse has been dragged a small bit, and allows MouseLeftButtonUp, etc. to be tracked. Is something else capturing the mouse? - Dave Clemmer
Late, but took me ages to find the what was causing this. Btw, I can confirm that the DoDragDrop handler code is executed even if the mouse didn't move! Using the below answer fixed my problems. Though it does mean I cannot use MVVM anymore for that particular button. - Roy T.

1 Answers

2
votes

If i understand you correctly, you don't want to drag&drop if you click the button. One way to do that is to hook up with the PreviewMouseLeftButtonDown of the button. Initiate you edit action in the eventhandler and set e.Handled=true. This way the MouseLeftButtonDown will not arrive at the listbox and no drag&drop operation will be initiated. One disadvantage is that the button will react on the mousedown instead of the mouseup (so it's like ClickMode=Press).

Regards, Rob