您的位置:首页 > 运维架构

WinForm DataGridView & WPF DataGrid 的拖拽(Drag & Drop)[z]

2012-09-04 18:35 543 查看
http://www.cnblogs.com/lichence/archive/2012/02/06/2340351.html



Download Source - 117KB






如上述两图,左侧为WinForm的DataGridView,右侧为WPF的DataGrid(封装成了自定义控件DragDataGrid,通过WPF的ElementHost装载在WinForm中)。均可以通过左键拽取行进行拖拽排序。另外实现了从WinForm的DataGridView向WPF的DataGrid的拖拽,因此,两个DataGridView之间的拖拽和两个DataGrid的拖拽方法类似。以上控件均关闭了Sort功能,并将AllowDrop属性置为true.

WinForm DataGridView的行拖拽使用Drag & Drop系列方法通过e.Data.GetData()来传递拽取的行数据,TipPanel的移动显示在MouseMove中控制

WPF DataGrid的行拖拽使用一个老外的Blog上的方法,点击这里

DataGridView 与 DataGrid之间的拖拽由于在WPF的DataGrid的DragEnter方法里,使用如下代码会报COM异常
1 DataGridViewRow curRow = (DataGridViewRow)e.Data.GetData(typeof(DataGridViewRow));
所以封装了一个数据序列化类DataSerializer将当前DataGridView 拖拽行的DataBoundItem序列化为string类型,在WPF中使用如下代码获取string后再反序列化为实例对象。
string strRow = (string)e.Data.GetData(DataFormats.StringFormat);
Animal aimAnimal = (Animal)DataSerializer.DeserializeObject(strRow);


TipControl & Popup在WinForm中使用了一个TipControl的UserControl显示拖拽过程中的行信息
在WPF中使用Popup显示当前行信息

按住Ctrl键再拖拽行,则实现数据行赋值功能。
存在的问题:还存在一些BUG和不足,个人学习,仅供参考。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: