WPF DataGrid Drag
自己实现的功能、代码比较简单的DataGrid的Drag处理,着重处理DataGrid里的拖动排序。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Windows; 6 using System.Windows.Controls; 7 using System.Windows.Data; 8 using System.Windows.Documents; 9 using System.Windows.Input; 10 using System.Windows.Media; 11 using System.Windows.Media.Imaging; 12 using System.Windows.Navigation; 13 using System.Windows.Shapes; 14 using System.Collections.ObjectModel; 15 16 namespace CodeTest.DataGridDragDrop 17 { 18 public partial class Page_DataGridDragDrop : Page 19 { 20 ObservableCollection<Model_DataGrid> list1; 21 ObservableCollection<Model_DataGrid> list2; 22 Point TargetMousePoint;//Drag时Mouse的Point 23 24 public Page_DataGridDragDrop() 25 { 26 InitializeComponent(); 27 InitializeDataGrid1(); 28 InitializeDataGrid2(); 29 } 30 private void InitializeDataGrid1() 31 { 32 list1 = new ObservableCollection<Model_DataGrid>(); 33 list1.Add(new Model_DataGrid { Id = 1, Name = "N1" }); 34 list1.Add(new Model_DataGrid { Id = 2, Name = "N2" }); 35 list1.Add(new Model_DataGrid { Id = 3, Name = "N3" }); 36 list1.Add(new Model_DataGrid { Id = 4, Name = "N4" }); 37 list1.Add(new Model_DataGrid { Id = 5, Name = "N5" }); 38 list1.Add(new Model_DataGrid { Id = 6, Name = "N6" }); 39 list1.Add(new Model_DataGrid { Id = 7, Name = "N7" }); 40 list1.Add(new Model_DataGrid { Id = 8, Name = "N8" }); 41 list1.Add(new Model_DataGrid { Id = 9, Name = "N9" }); 42 this.DataGrid1.ItemsSource = list1; 43 } 44 private void InitializeDataGrid2() 45 { 46 list2 = new ObservableCollection<Model_DataGrid>(); 47 list2.Add(new Model_DataGrid { Id = 1, Name = "Na1" }); 48 list2.Add(new Model_DataGrid { Id = 2, Name = "Na2" }); 49 list2.Add(new Model_DataGrid { Id = 3, Name = "Na3" }); 50 list2.Add(new Model_DataGrid { Id = 4, Name = "Na4" }); 51 list2.Add(new Model_DataGrid { Id = 5, Name = "Na5" }); 52 list2.Add(new Model_DataGrid { Id = 6, Name = "Na6" }); 53 this.DataGrid2.ItemsSource = list2; 54 } 55 /// <summary> 56 /// 拖动处理 57 /// </summary> 58 /// <param name="sender"></param> 59 /// <param name="e"></param> 60 private void DataGrid1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 61 { 62 Model_DataGrid DraggedItem = null;//源Row 63 Model_DataGrid TargetItem = null;//目标Row 64 //查找鼠标点击的源Row 65 IInputElement element = DataGrid1.InputHitTest(e.GetPosition(DataGrid1)); 66 while(element != DataGrid1) 67 { 68 if(element != null && element is DataGridRow) 69 { 70 DataGrid1.SelectedItem = ((DataGridRow)element).Item; 71 DraggedItem = (Model_DataGrid)DataGrid1.SelectedItem; 72 break; 73 } 74 else 75 { 76 DataGrid1.SelectedItem = null; 77 element = System.Windows.Media.VisualTreeHelper.GetParent(element as System.Windows.DependencyObject) as System.Windows.IInputElement; 78 } 79 } 80 81 if(this.DataGrid1.SelectedCells.Count > 0) 82 { 83 Model_DataGrid DragData = this.DataGrid1.SelectedCells[0].Item as Model_DataGrid; 84 DragDrop.DoDragDrop(DataGrid1, DragData, DragDropEffects.Move); 85 //拖动结束 86 element = DataGrid1.InputHitTest(TargetMousePoint); 87 while (element != DataGrid1) 88 { 89 if (element != null && element is DataGridRow) 90 { 91 TargetItem = (Model_DataGrid)((DataGridRow)element).Item; 92 break; 93 } 94 else 95 { 96 element = System.Windows.Media.VisualTreeHelper.GetParent(element as System.Windows.DependencyObject) as System.Windows.IInputElement; 97 } 98 } 99 //处理排序 100 if (TargetItem != null && !ReferenceEquals(DraggedItem, TargetItem)) 101 { 102 //remove the source from the list 103 list1.Remove(DraggedItem); 104 105 //get target index 106 var targetIndex = list1.IndexOf(TargetItem); 107 108 //move source at the target's location 109 list1.Insert(targetIndex, DraggedItem); 110 111 //select the dropped item 112 DataGrid1.SelectedItem = DraggedItem; 113 } 114 } 115 } 116 117 private void DataGrid2_Drop(object sender, DragEventArgs e) 118 { 119 IDataObject data = new DataObject(); 120 data = e.Data; 121 Model_DataGrid obj = (Model_DataGrid)data.GetData(typeof(Model_DataGrid)); 122 Console.WriteLine(obj.Name); 123 } 124 /// <summary> 125 /// 获取拖动结束时鼠标的Point 126 /// </summary> 127 /// <param name="sender"></param> 128 /// <param name="e"></param> 129 private void DataGrid1_DragOver(object sender, DragEventArgs e) 130 { 131 TargetMousePoint = e.GetPosition(DataGrid1); 132 } 133 } 134 }