WPF DataGrid Drag

时间:2023-11-10 13:27:44

自己实现的功能、代码比较简单的DataGrid的Drag处理,着重处理DataGrid里的拖动排序。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel; namespace CodeTest.DataGridDragDrop
{
public partial class Page_DataGridDragDrop : Page
{
ObservableCollection<Model_DataGrid> list1;
ObservableCollection<Model_DataGrid> list2;
Point TargetMousePoint;//Drag时Mouse的Point public Page_DataGridDragDrop()
{
InitializeComponent();
InitializeDataGrid1();
InitializeDataGrid2();
}
private void InitializeDataGrid1()
{
list1 = new ObservableCollection<Model_DataGrid>();
list1.Add(new Model_DataGrid { Id = , Name = "N1" });
list1.Add(new Model_DataGrid { Id = , Name = "N2" });
list1.Add(new Model_DataGrid { Id = , Name = "N3" });
list1.Add(new Model_DataGrid { Id = , Name = "N4" });
list1.Add(new Model_DataGrid { Id = , Name = "N5" });
list1.Add(new Model_DataGrid { Id = , Name = "N6" });
list1.Add(new Model_DataGrid { Id = , Name = "N7" });
list1.Add(new Model_DataGrid { Id = , Name = "N8" });
list1.Add(new Model_DataGrid { Id = , Name = "N9" });
this.DataGrid1.ItemsSource = list1;
}
private void InitializeDataGrid2()
{
list2 = new ObservableCollection<Model_DataGrid>();
list2.Add(new Model_DataGrid { Id = , Name = "Na1" });
list2.Add(new Model_DataGrid { Id = , Name = "Na2" });
list2.Add(new Model_DataGrid { Id = , Name = "Na3" });
list2.Add(new Model_DataGrid { Id = , Name = "Na4" });
list2.Add(new Model_DataGrid { Id = , Name = "Na5" });
list2.Add(new Model_DataGrid { Id = , Name = "Na6" });
this.DataGrid2.ItemsSource = list2;
}
/// <summary>
/// 拖动处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DataGrid1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Model_DataGrid DraggedItem = null;//源Row
Model_DataGrid TargetItem = null;//目标Row
//查找鼠标点击的源Row
IInputElement element = DataGrid1.InputHitTest(e.GetPosition(DataGrid1));
while(element != DataGrid1)
{
if(element != null && element is DataGridRow)
{
DataGrid1.SelectedItem = ((DataGridRow)element).Item;
DraggedItem = (Model_DataGrid)DataGrid1.SelectedItem;
break;
}
else
{
DataGrid1.SelectedItem = null;
element = System.Windows.Media.VisualTreeHelper.GetParent(element as System.Windows.DependencyObject) as System.Windows.IInputElement;
}
} if(this.DataGrid1.SelectedCells.Count > )
{
Model_DataGrid DragData = this.DataGrid1.SelectedCells[].Item as Model_DataGrid;
DragDrop.DoDragDrop(DataGrid1, DragData, DragDropEffects.Move);
//拖动结束
element = DataGrid1.InputHitTest(TargetMousePoint);
while (element != DataGrid1)
{
if (element != null && element is DataGridRow)
{
TargetItem = (Model_DataGrid)((DataGridRow)element).Item;
break;
}
else
{
element = System.Windows.Media.VisualTreeHelper.GetParent(element as System.Windows.DependencyObject) as System.Windows.IInputElement;
}
}
//处理排序
if (TargetItem != null && !ReferenceEquals(DraggedItem, TargetItem))
{
//remove the source from the list
list1.Remove(DraggedItem); //get target index
var targetIndex = list1.IndexOf(TargetItem); //move source at the target's location
list1.Insert(targetIndex, DraggedItem); //select the dropped item
DataGrid1.SelectedItem = DraggedItem;
}
}
} private void DataGrid2_Drop(object sender, DragEventArgs e)
{
IDataObject data = new DataObject();
data = e.Data;
Model_DataGrid obj = (Model_DataGrid)data.GetData(typeof(Model_DataGrid));
Console.WriteLine(obj.Name);
}
/// <summary>
/// 获取拖动结束时鼠标的Point
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DataGrid1_DragOver(object sender, DragEventArgs e)
{
TargetMousePoint = e.GetPosition(DataGrid1);
}
}
}