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


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的行拖拽
  • 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和不足,个人学习,仅供参考。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM