WPF Beginner - Drag&Drop Item in listview

How to drag&Drop listview items?

The first thing come to my mind is that, to use evensetter. However, it is not true. The Drag & Drop event is never triggered.

Then I searched and found a log of articles. The famous one is: http://www.codeproject.com/KB/WPF/ListViewDragDropManager.aspx.
It is using PreviewMouseButtonDown, PreviewMouseMove, PreviewMouseLeftButtonUp, to track the mouse activities. And some tricks are also used, for example, interropt system32 API, defining thread hold of "Drag".

1. It is using PreviewMouseButtonDown, PreviewMouseMove, PreviewMouseLeftButton down. to track the mouse activities. why use preview events not bubble events? 
Check MSDN, and my previous log, it is said clearly: A lot of control has suppressed MouseLeftButtonDown. If you still want to capture this event, use previewlmosuebutton down.

2. I modified the interupt API, use MouseEvent.GetPosition() instead.

3. The Adorner thing, i am using MSDN sample, http://blogs.msdn.com/marcelolr/archive/2006/03/03/showing-drag-drop-feedback-on-the-wpf-adorner-layer.aspx instead. it is much clearly.

Follownig are some code snippets, a lot of bugs...

 
void ListView_PreviewMouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)

{

     if (_isDown)

    {

         DragFinished(e.GetPosition(listView));

         e.Handled = true;

    }

}

 

void ListView_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)

{

    if (_isDown)

   {

        if ((_isDragging == false) &&

            ((Math.Abs(e.GetPosition(listView).X - _startPoint.X) > SystemParameters.MinimumHorizontalDragDistance)

           || (Math.Abs(e.GetPosition(listView).Y - _startPoint.Y) > SystemParameters.MinimumVerticalDragDistance)))

     {

           DragStarted();

     }

     if (_isDragging)

   {

          DragMoved();

    }

  }

}

 

void ListView_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)

{

   _currentDragItem = IndexUnderDragCursor;

if (_currentDragItem < 0)

return;

ListViewItem itemToDrag = this.GetListViewItem(_currentDragItem);

if (itemToDrag == null)

return;

_isDown = true;

_startPoint = e.GetPosition(listView);

_originalElement = itemToDrag;

itemToDrag.CaptureMouse();

e.Handled = true;

}


 

posted @ 2009-08-26 17:36  Pang pang Xiong  阅读(725)  评论(0编辑  收藏  举报