首页 技术 正文
技术 2022年11月15日
0 收藏 851 点赞 2,737 浏览 3485 个字

自己实现的功能、代码比较简单的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);
}
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,893