WPF Reflections

Nov 9 2007   9:23AM GMT

Drag and drop – checking mouse button



Posted by: MarkWPF
Tags:
WPF

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 :-)

An example:

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;
}
else
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

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: