首先设置ListView的AllowDrop=True;SelectionMode=Extended;并且ListView视图为GridVIew。

 private void listView1_MouseMove(object sender, MouseEventArgs e)
{
ListView listview = sender as ListView;
if (e.LeftButton == MouseButtonState.Pressed)
{
System.Collections.IList list = listview.SelectedItems as System.Collections.IList;
DataObject data = new DataObject(typeof(System.Collections.IList), list);
if (list.Count > )
{
DragDrop.DoDragDrop(listview, data, DragDropEffects.Move);
}
}
} private void listView1_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(System.Collections.IList)))
{
System.Collections.IList peopleList = e.Data.GetData(typeof(System.Collections.IList)) as System.Collections.IList;
//index为放置时鼠标下元素项的索引
int index = GetCurrentIndex(new GetPositionDelegate(e.GetPosition));
if (index > -)
{
LogMessage Logmess = peopleList[] as LogMessage;
//拖动元素集合的第一个元素索引
int OldFirstIndex = StreamHelper.List.IndexOf(Logmess);
//下边那个循环要求数据源必须为ObservableCollection<T>类型,T为对象
for (int i = ; i < peopleList.Count; i++)
{
StreamHelper.List.Move(OldFirstIndex, index);
}
listView1.SelectedItems.Clear();
}
}
} private int GetCurrentIndex(GetPositionDelegate getPosition)
{
int index = -;
for (int i = ; i < listView1.Items.Count; ++i)
{
ListViewItem item = GetListViewItem(i);
if (item != null && this.IsMouseOverTarget(item, getPosition))
{
index = i;
break;
}
}
return index;
} private bool IsMouseOverTarget(Visual target, GetPositionDelegate getPosition)
{
Rect bounds = VisualTreeHelper.GetDescendantBounds(target);
Point mousePos = getPosition((IInputElement)target);
return bounds.Contains(mousePos);
} delegate Point GetPositionDelegate(IInputElement element); ListViewItem GetListViewItem(int index)
{
if (listView1.ItemContainerGenerator.Status != GeneratorStatus.ContainersGenerated)
return null;
return listView1.ItemContainerGenerator.ContainerFromIndex(index) as ListViewItem;
}

其中StreamHelper.List是StreamHelper类里边有一个静态的ObservableCollection<LogMessage>集合名字为List;

LogMessage就是集合里边的T对象了;

WPF ListView 简单的拖拽实现(转)的更多相关文章

  1. 【WPF】一些拖拽实现方法的总结(Window,UserControl)

    原文:[WPF]一些拖拽实现方法的总结(Window,UserControl) 原文地址 https://www.cnblogs.com/younShieh/p/10811456.html 前文 本文 ...

  2. 【转】C#.net拖拽实现获得文件路径

    C#.net拖拽实现获得文件路径 作者Attilax ,  EMAIL:1466519819@qq.com 思路: 通过DragEnter事件获得被拖入窗口的“信息”(可以是若干文件,一些文字等等), ...

  3. 【WPF】这可能是全网最全的拖拽实现方法的总结

    原文地址 https://www.cnblogs.com/younShieh/p/10811456.html 前文 本文只对笔者学习掌握的一般的拖动问题的实现方法进行整理和讨论,包括窗口.控件等内容的 ...

  4. html5简单拖拽实现自动左右贴边+幸运大转盘

    此篇文章主要实现两个功能: 1.点击屏幕下方签到悬浮按钮: 2.弹出幸运大转盘,转盘抽奖签到 效果如图: ​ 在网上找了很多移动端拖拽的js实现方式,大部分都是这一种,html5的touch事件,但是 ...

  5. android自定义控件(2)-拖拽实现开关切换

    在这里,我们的主要工作就是在原有代码的基础上,增加一个重写的onTouchEvent方法,刚添加上来的时候是这个样子的: @Override public boolean onTouchEvent(M ...

  6. C#.net拖拽实现获得文件路径

    思路: 通过DragEnter事件获得被拖入窗口的“信息”(可以是若干文件,一些文字等等),  在DragDrop事件中对“信息”进行解析.        窗体的AllowDrop属性必须设置成tru ...

  7. HTML5元素拖拽实现示例

    HTML5现在前端圈中,已然成为一个不那么新的技术词汇了,很多公司也把HTML5也当成了硬性的技能要求,但是很多前端恐怕都不了解HTML5的拖拽怎么实现吧. 看了下极客学院的视频,大概的了解了下思路. ...

  8. Silverlight中的拖拽实现的图片上传

    原文 http://blog.csdn.net/dujingjing1230/article/details/5443003 在Silverlight中因为可以直接从系统的文件夹里面拖出来一个文件直接 ...

  9. selenium操作拖拽实现无效果的替代方案

    如果碰到这种情况,无论你是直接通过draganddrop()还是分步执行clickandhold(),dragtoelement(),或通过by_offset位移都无法实现元素拖拽.只能物理模拟了 w ...

随机推荐

  1. AIX5L内存监控和调整

    1.ps ps gv | head -n 1; ps gv | egrep -v "RSS" | sort +6b -7 -n -r PID      TTY    STAT    ...

  2. mysql监控利器mysqlmtop部署安装

    MySQLMTOP是一个由Python+PHP开发的MySQL企业级监控系统.系统由Python实现多进程数据采集和告警,PHP实现WEB展示和管理.最重要是MySQL服务器无需安装任何Agent,只 ...

  3. CentOS6编译安装Python3

    CentOS6默认Python版本是2, 但我使用的是Python3,因此需要自己安装 但记住,不要删除自带的Python2,否则很多东西会报错,比如yum之类的.(别问我为什么知道...) 话不多说 ...

  4. 比较强大 优秀的开源框架 :Android图片加载与缓存:Android Glide 的用法

    使用Android Glide,需要先下载Android Glide的库,Android Glide在github上的项目主页: https://github.com/bumptech/glide . ...

  5. Disabling the Windows 8 UAC

    http://www.computerperformance.co.uk/win8/windows8-uac-disable.htm https://msdn.microsoft.com/en-us/ ...

  6. Java——多线程常见面试题

    body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...

  7. Spring学习笔记之Container overview

    The Spring IoC container

  8. L243 词汇题2009

    The applications of genetic engineering are abundant (plentiful) and choosing one appropriate for th ...

  9. php 值引用

    1.值传递 复制代码 代码如下: <?php function exam($var1){ $var1++: echo "In Exam:" . $var1 . "& ...

  10. 负margin

    负margin理论: 何谓参考线?参考线就是 margin移动的基准点,此基准点相对于box(自身)是静止的.而margin的数值,就是box相对于参考线的位移量. 一个完整的margin属性是这么写 ...