Posted by: MarkWPF
If you are doing drag and drop by hand (rather than using Josh Smith’s excellent DragAndDropManager – http://www.codeproject.com/WPF/ListViewDragDropManager.asp) for whatever reason, one thing to be careful is to to allow existing functionality to carry on working.
For example, in a listview, you want the ability to drag and drop column headers (to move columns around) to continue, if it has been turned on by setting AllowsColumnReorder=”True”.
Therefore you need to check that when the left mouse button is pressed, you only specify it as a drag and drop button click if you are sure it’s a listviewitem being selected, rather than a scrollbar or a header.
You could walk up the visual tree looking for the right type of control, or look for a property being set somewhere in the visual tree. However, what I have found useful is to check that the originating element’s datacontext is set to the type of the listviewitem’s bound type. This only works if you’re using databinding and an itemscontrol or descendent (listbox or listview), but is quick and easy
private void lv_PreviewMouseLeftButtonDown(object sender, MouseEventArgs e)
// check to see if drag can be done
FrameworkElement ele = e.OriginalSource as FrameworkElement;
if(ele != null && ele.DataContext != null && ele.DataContext.GetType() == typeof(xxx))
m_StartPoint = e.GetPosition(null); // save mouse position
m_IsDragging = false;
m_IsDragging = true;
where m_IsDragging is a boolean indicating whether you can do drag and drop operation, and m_StartPoint is used by the drag and drop operation to test whether to start doing it, and xxx is your object that is bound to the listview