现在的位置: 首页 > 综合 > 正文

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

2013年07月08日 ⁄ 综合 ⁄ 共 899字 ⁄ 字号 评论关闭

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

抱歉!评论已关闭.