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;

}


 

转载于:https://www.cnblogs.com/pangpangxiong/archive/2009/08/26/1554400.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值