自己实现的功能、代码比较简单的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的更多相关文章

  1. xceed wpf datagrid

    <!--*********************************************************************************** Extended ...

  2. WPF DataGrid常用属性记录

    WPF DataGrid常用属性记录 组件常用方法: BeginEdit:使DataGrid进入编辑状态. CancelEdit:取消DataGrid的编辑状态. CollapseRowGroup:闭 ...

  3. WPF DATAGRID - COMMITTING CHANGES CELL-BY-CELL

    In my recent codeproject article on the DataGrid I described a number of techniques for handling the ...

  4. WPF DataGrid某列使用多绑定后该列排序失效,列上加入 SortMemberPath 设置即可.

    WPF DataGrid某列使用多绑定后该列排序失效 2011-07-14 10:59hdongq | 浏览 1031 次  悬赏:20 在wpf的datagrid中某一列使用了多绑定,但是该列排序失 ...

  5. 获取wpf datagrid当前被编辑单元格的内容

    原文 获取wpf datagrid当前被编辑单元格的内容 确认修改单元个的值, 使用到datagrid的两个事件 开始编辑事件 BeginningEdit="dataGrid_Beginni ...

  6. WPF DataGrid绑定一个组合列

    WPF DataGrid绑定一个组合列 前台: <Page.Resources>        <local:InfoConverter x:Key="converter& ...

  7. WPF DataGrid自定义样式

    微软的WPF DataGrid中有很多的属性和样式,你可以调整,以寻找合适的(如果你是一名设计师).下面,找到我的小抄造型的网格.它不是100%全面,但它可以让你走得很远,有一些非常有用的技巧和陷阱. ...

  8. WPF DataGrid显格式

    Guide to WPF DataGrid formatting using bindings Peter Huber SG, 25 Nov 2013 CPOL    4.83 (13 votes) ...

  9. WPF DataGrid Custommization using Style and Template

    WPF DataGrid Custommization using Style and Template 代码下载:http://download.csdn.net/detail/wujicai/81 ...

随机推荐

  1. 常用SQL DDL语句

    常用SQL DDL语句 DDL-数据库定义语言:直接提交的.CREATE:用于创建数据库对象.DECLARE:除了是创建只在过程中使用的临时表外,DECLARE语句和CREATE语句非常相似.唯一可以 ...

  2. 全局文件 pch

    在 bulding setting 里面 搜 prefix header 然后添加自己的pch 路径, 类似 $(SRCROOT)/... 还要把 precompile prefix header 设 ...

  3. 如何在Windows Server 2016启用或关闭Internet Explorer增强的安全配置

    一般我们安装完服务器后,开启 Internet Explorer 会发现无法上网或者上网内容被屏蔽掉了 问题的发生原因 在 Windows Server 2016 通常扮演重要的服务器角色,不应该用来 ...

  4. easyui弹出窗关闭前调用确认窗口,先关闭页面后调用弹出窗口

    弹出窗关闭的时候提示是否关闭,同时进行一些对应的方法调用, 然而在进行页面关闭调用的时候,往往页面关闭了,才弹出确认对话框, $.messager.confirm和panel的onBeforeClos ...

  5. 高斯RBF核函数中Sigma取值和SVM分离面的影响

    1:高斯RBF核函数的定义 k(x) = exp(-x^2/(2×sigma)) 在MATLAB中输入一下代码:ezsurf('exp(-x^2/(2*sigma^2))'); 在GOOGLE中输入“ ...

  6. swift 协议传值的实现

    首先呢说下结构 一个ViewController 一个ModelViewController 在ModelViewController中定义了一个协议 这个逻辑 从第一个界面进入第二个界面  从第二个 ...

  7. Redhat6.4下配置本地yum

    一.准备工作1. Linux安装盘插入光驱 2. 挂载光驱 [root@localhost ~]# mount /dev/cdrom /mnt/  mount: block device /dev/s ...

  8. Java线程:同步

    一 同步的概念 线程的同步是为了防止多个线程访问一个数据对象时,对数据造成的破坏. 例如:两个线程ThreadA.ThreadB都操作同一个对象Foo对象,并修改Foo对象上的数据. MyRunnab ...

  9. JQuery hover toggle事件使用

    JQuery hover toggle事件使用: <%@ page language="java" import="java.util.*" pageEn ...

  10. js控制公共模板中,不同页面中的导航选中效果-判断当前的url

    用js的做法也很多.比较推荐的方法是判断当前的url,然后根据url在nav中的位置,来对nav中的某个导航增加选中样式,代码如下: <!doctype html> <html la ...