WPF Drag and Drop blocking operations
I am working with a WPF application that is using Drag and Drop f开发者_StackOverflow中文版unctionality.
The Drag and Drop operation is a blocking operation and is having some negative side effects in my application. I have recently added the use of an adorner to show the item dragging. The problem with this is that in order to do this, I need to track the current position of the mouse. When a Drag and Drop operation is initiated, it blocks further execution until the item is dropped.
I have read that a fix for this is to execute the drag and drop in its own thread, and then update the UI. I read this article here
http://msdn.microsoft.com/en-us/library/ms741870.aspx
I am not sure if this is what I am looking to do, but It sounds like what I need.
Is there another fix around this?
Here is the code that I need to execute.
private void FieldItemGrid_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (_isDown)
{
if ((_isDragging == false))
{
/*Add Adorner to Item that is being dragged*/
DragStarted(e.GetPosition(this));
}
if (_selectedElement != null)
{
/*Begin Drag Operation*/
DragDrop.DoDragDrop(_selectedElement, _selectedElement, DragDropEffects.Move);
}
/*The following code is not executed until the dragged item is released*/
if (_isDragging)
{
/*Update Current Position of Mouse to update adorner position*/
DragMoved(e.GetPosition(this));
}
}
}
You can use DragDrop.GiveFeedback
attached event for that:
private void FieldItemGrid_PreviewMouseMove(object sender, MouseEventArgs e) {
if (_isDown) {
if ((_isDragging == false)) {
/*Add Adorner to Item that is being dragged*/
DragStarted(e.GetPosition(this));
}
if (_selectedElement != null) {
DragDrop.AddGiveFeedbackHandler(Element, OnGiveFeedback);
try {
/*Begin Drag Operation*/
DragDrop.DoDragDrop(_selectedElement, _selectedElement, DragDropEffects.Move);
}
finally {
DragDrop.RemoveGiveFeedbackHandler(Element, OnGiveFeedback);
}
}
/*The following code is not executed until the dragged item is released*/
if (_isDragging) {
/*Update Current Position of Mouse to update adorner position*/
DragMoved(e.GetPosition(this));
}
}
}
private void OnGiveFeedback(object sender, GiveFeedbackEventArgs e) {
// Update adorner location here
}
精彩评论