WPF DataGrid Drag
自己实现的功能、代码比较简单的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);
}
}
}
WPF DataGrid Drag的更多相关文章
- xceed wpf datagrid
<!--*********************************************************************************** Extended ...
- WPF DataGrid常用属性记录
WPF DataGrid常用属性记录 组件常用方法: BeginEdit:使DataGrid进入编辑状态. CancelEdit:取消DataGrid的编辑状态. CollapseRowGroup:闭 ...
- WPF DATAGRID - COMMITTING CHANGES CELL-BY-CELL
In my recent codeproject article on the DataGrid I described a number of techniques for handling the ...
- WPF DataGrid某列使用多绑定后该列排序失效,列上加入 SortMemberPath 设置即可.
WPF DataGrid某列使用多绑定后该列排序失效 2011-07-14 10:59hdongq | 浏览 1031 次 悬赏:20 在wpf的datagrid中某一列使用了多绑定,但是该列排序失 ...
- 获取wpf datagrid当前被编辑单元格的内容
原文 获取wpf datagrid当前被编辑单元格的内容 确认修改单元个的值, 使用到datagrid的两个事件 开始编辑事件 BeginningEdit="dataGrid_Beginni ...
- WPF DataGrid绑定一个组合列
WPF DataGrid绑定一个组合列 前台: <Page.Resources> <local:InfoConverter x:Key="converter& ...
- WPF DataGrid自定义样式
微软的WPF DataGrid中有很多的属性和样式,你可以调整,以寻找合适的(如果你是一名设计师).下面,找到我的小抄造型的网格.它不是100%全面,但它可以让你走得很远,有一些非常有用的技巧和陷阱. ...
- WPF DataGrid显格式
Guide to WPF DataGrid formatting using bindings Peter Huber SG, 25 Nov 2013 CPOL 4.83 (13 votes) ...
- WPF DataGrid Custommization using Style and Template
WPF DataGrid Custommization using Style and Template 代码下载:http://download.csdn.net/detail/wujicai/81 ...
随机推荐
- pku2104
传送门:http://poj.org/problem?id=2104 题目大意:给定一个长度为N的数组{A[i]},你的任务是解决Q个询问.每次询问在A[l], A[l+1], ...... , A[ ...
- zip-auto.sh
#!/bin/sh #auto zip package #Define Path #####test######### mkdir -p /root/shell/test1 /root/shell/t ...
- CodeForces 755C PolandBall and Forest (并查集)
题意:给定每一点离他最远的点,问是这个森林里有多少棵树. 析:并查集,最后统计不同根结点的数目即可. 代码如下: #pragma comment(linker, "/STACK:102400 ...
- Python3基础 lambda表达式 简单示例
镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.-------------------------------------- ...
- 中英文混合字符串截取java
//截取字符串长度(中文2个字节,半个中文显示一个) public String subTextString(String str,int len){ if(str.length()<len/2 ...
- centos 6.5下编译安装、配置高性能服务器Nginx
1.nginx是什么? Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,由俄罗斯的程序设计师Igor Sysoev所开发,其特点是占有内存少,并发能力 ...
- Mac 安装Rudy环境 pod安装前的准备工作
之前已经说过怎么使用pod 今天说一下安装pod之前的准备工作 首先呢就是Rudy 环境(前提是你已经安装了Xcode) 在终端输入一下命令 期间可能也许会要你输入密码 curl -L https:/ ...
- python如何安装模块
1.从 https://pypi.python.org/pypi/XXXX 下载压缩包 2.解压所下载的压缩包 3.CD到解压目录,执行 sudo python setup.py install
- ubuntu开机自动关闭独显,使用集成显卡
我的本子是联想y470p-ise,因为是有双显卡,而ubuntu在开机后,双显卡默认是同时工作,会产生巨大的发热,导致很不爽.而且在ubuntu下基本我也不用独显,所以有开机关闭独显的需求. ubun ...
- yum安装CDH5.5 Hadoop集群
1.环境说明 系统环境: 系统环境:centos6.7 Hadoop版本:CDH5.5 JDK运行版本:1.7.0_67 集群各节点组件分配: 2.准备工作 安装 Hadoop 集群前先做好下面的准备 ...