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 ...
随机推荐
- 转化成maven dependencis
右键工程--->configure--->convert to maven project
- 创建iwatch 程序选项
include complication :包含自定义表盘事件 include glance scene:包含缩略图事件
- osgEarth编译(转载)
osgEarth编译 osgEarth的编译需要osg和一些第三方插件库,我主要参考了cnblogs上的一篇博文,但是也不够详细,并且我是在已经编译好osg的情况下去编译osgEarth,所以期间也遇 ...
- PHP 单态设计模式复习
单态设计模式,也可以叫做单例设计模式, 就是一个类只能让它生成一个对象,避免重复的NEW,影响运行效率(每NEW一个对象都会在内存中开辟一块空间) 示例代码 <?php /* * 单态设计模式 ...
- 2.14. 删除托管对象(Core Data 应用程序实践指南)
删除托管对象,只要调用托管对象上下文的deleteObject 或 deleteObjects就可以了.同样,真正的删除,要在调用save:之后.
- Linux CentOS 安装 httpd
1.查看并安装服务器是否安装编译器 make gcc gcc-c++ 查看:rpm -q gcc-c++ 2.查看SELinux 和 iptables 的状态 3.在根目录新建文件夹 lamp 4.使 ...
- hadoop-1.x的运行实例
我的环境是hadoop-0.20.2,eclipse:SDK-3.3.2, 源数据为: Apr 23 11:49:54 hostapd: wlan0: STA 14:7d:c5:9e:fb:84 Ap ...
- JavaScript 模拟策略模式
/** * 模拟一个接口,其方法会抛出异常: */ function FlyInter () {} FlyInter.prototype.fly = function() { throw '实现这个接 ...
- 在Java中打开浏览器
try { Runtime.getRuntime().exec( "explorer http://www.baidu.com"); java.net.URI uri = new ...
- RESTful_简介
一.概括总结一下什么是RESTful架构: (1)每一个URI代表一种资源: (2)客户端和服务器之间,传递这种资源的某种表现层(Representation): (3)客户端通过四个HTTP动词,对 ...