DataGrid 拖动 附加属性类
项目需要实现一个DataGrid拖动排序,于是参考网上一些资源然后,修改了下实现了一个附加属性类,如下
使用方法
<DataGrid
x:Name="shareGrid"
test:DragDropRowBehavior.Enabled="True"
test:DragDropRowBehavior.DropFinish="{Binding TestCommand}" />
DropFinish有在鼠标释放时调用,参数中包含了 拖动的数据,行号,目标行号,目标数据等。
源码:
public static class DataGridDragDropRowBehavior
{
public delegate Point GetDragDropPosition(IInputElement theElement); public static DependencyProperty DropFinishProperty =
DependencyProperty.RegisterAttached("DropFinish", typeof(ICommand), typeof(DataGridDragDropRowBehavior),
new UIPropertyMetadata(null)); public static void SetDropFinish(UIElement target, ICommand value)
{
target.SetValue(DropFinishProperty, value);
} public static bool GetEnabled(DependencyObject obj)
{
return (bool)obj.GetValue(EnabledProperty);
} public static void SetEnabled(DependencyObject obj, bool value)
{
obj.SetValue(EnabledProperty, value);
} public static readonly DependencyProperty EnabledProperty =
DependencyProperty.RegisterAttached("Enabled", typeof(bool), typeof(DataGridDragDropRowBehavior), new PropertyMetadata(false, OnEnableChanged)); private static void OnEnableChanged(DependencyObject depObject, DependencyPropertyChangedEventArgs e)
{
var dataGrid = depObject as DataGrid; var enable = (bool)e.NewValue;
if (enable)
{
dataGrid.AllowDrop = true;
dataGrid.PreviewMouseLeftButtonDown += DataGrid_PreviewMouseLeftButtonDown;
dataGrid.Drop += DataGrid_Drop;
}
else
{
dataGrid.PreviewMouseLeftButtonDown -= DataGrid_PreviewMouseLeftButtonDown;
dataGrid.Drop -= DataGrid_Drop;
}
} private static void DataGrid_Drop(object sender, DragEventArgs e)
{
DragItem dragitem = e.Data.GetData("DragItem") as DragItem; if (dragitem.RowIndex < )
{
return;
}
DataGrid datagrid = sender as DataGrid;
int index = GetDataGridItemCurrentRowIndex(e.GetPosition, datagrid); //The current Rowindex is -1 (No selected)
if (index < )
{
return;
}
//If Drag-Drop Location are same
if (index == dragitem.RowIndex)
{
return;
} var targetItem = datagrid.Items[index]; DropEventArgs arg = new DropEventArgs();
arg.SourceRowIndex = dragitem.RowIndex;
arg.TargetRowIndex = index;
arg.Data = dragitem.Data;
arg.TargetData = targetItem; var command = (ICommand)datagrid.GetValue(DropFinishProperty);
if (command != null)
{
command.Execute(arg);
}
} private static void DataGrid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DataGrid datagrid = sender as DataGrid;
var m_prevRowIndex = GetDataGridItemCurrentRowIndex(e.GetPosition, datagrid); if (m_prevRowIndex < )
{
return;
}
datagrid.SelectedIndex = m_prevRowIndex; var selectedItem = datagrid.Items[m_prevRowIndex]; if (selectedItem == null)
{
return;
} DragItem dragItem = new DragItem();
dragItem.RowIndex = m_prevRowIndex;
dragItem.Data = selectedItem;
DataObject data = new DataObject("DragItem", dragItem);
//Now Create a Drag Rectangle with Mouse Drag-Effect
//Here you can select the Effect as per your choice DragDropEffects dragdropeffects = DragDropEffects.Move; if (DragDrop.DoDragDrop(datagrid, data, dragdropeffects) != DragDropEffects.None)
{
//Now This Item will be dropped at new location and so the new Selected Item
datagrid.SelectedItem = selectedItem;
}
} /// <summary>
/// Method checks whether the mouse is on the required Target
/// Input Parameter (1) "Visual" -> Used to provide Rendering support to WPF
/// Input Paraneter (2) "User Defined Delegate" positioning for Operation
/// </summary>
/// <param name="theTarget"></param>
/// <param name="pos"></param>
/// <returns>The "Rect" Information for specific Position</returns>
private static bool IsTheMouseOnTargetRow(Visual theTarget, GetDragDropPosition pos)
{
Rect posBounds = VisualTreeHelper.GetDescendantBounds(theTarget);
Point theMousePos = pos((IInputElement)theTarget);
return posBounds.Contains(theMousePos);
} private static DataGridRow GetDataGridRowItem(DataGrid dataGrid, int index)
{
if (dataGrid.ItemContainerGenerator.Status != GeneratorStatus.ContainersGenerated)
{
return null;
}
return dataGrid.ItemContainerGenerator.ContainerFromIndex(index)
as DataGridRow;
} private static int GetDataGridItemCurrentRowIndex(GetDragDropPosition pos, DataGrid dataGrid)
{
int curIndex = -;
for (int i = ; i < dataGrid.Items.Count; i++)
{
DataGridRow itm = GetDataGridRowItem(dataGrid, i);
if (IsTheMouseOnTargetRow(itm, pos))
{
curIndex = i;
break;
}
}
return curIndex;
}
} public class DragItem
{
public int RowIndex { get; set; }
public object Data { get; set; }
} public class DropEventArgs
{
public int SourceRowIndex { get; set; }
public object Data { get; set; } public int TargetRowIndex { get; set; } public object TargetData { get; set; }
}
参考资源:http://www.dotnetcurry.com/wpf/677/wpf-data-grid-row-drag-drop
DataGrid 拖动 附加属性类的更多相关文章
- silverlight datagrid绑定匿名类
原文 http://www.cnblogs.com/luweis/archive/2011/10/21/2220587.html 刚开始遇到的一个问题是这样的,我有一个datagrid,根据不同的条件 ...
- datagrid拖动列头更换排列顺序
在做这个功能的时候在网上找了大量资料,发现都不适用,要不然就是代码太冗余,所以另起炉灶,自己封装了这个函数 下面是完整的代码: <!DOCTYPE html> <html> & ...
- 控制EasyUI DataGrid高度
这次要说的是控制EasyUI的高度,平时我公司的项目,用EasyUI较多,然后datagrid这个组件是用的非常多的.平时我们都是固定高度,常见代码如下: <table ...
- VS2015中的项目类图
发现右键项目的时候,是没有类图的. https://msdn.microsoft.com/en-us/library/hyxd8c85.aspx 右键项目--添加--新建项. 选择类图. 然后将整个项 ...
- Flutter进阶—点击、拖动和其他手势
Flutter中的手势系统有两个层次.第一层具有原始指针事件,其描述了穿过屏幕的指针(例如触摸.鼠标和触控笔)的位置和移动.第二层具有手势,其描述由一个或多个指针移动组成的语义动作. 指针指针代表用户 ...
- Windows Community Toolkit 4.0 - DataGrid - Part03
概述 在上面一篇 Windows Community Toolkit 4.0 - DataGrid - Part02 中,我们针对 DataGrid 控件的 Utilities 部分做了详细分享.而在 ...
- C# WinForm开发系列 - DataGrid/DataGridView
在WinForm开发中,DataGrid/DataGridView被广泛使用于绑定数据库中数据进行呈现.整理一些关于DataGrid/DataGridView使用的文章,涉及DataGrid/Data ...
- easyui datagrid列拖拽
<script type="text/javascript"> var cols = [{ field: 'testName', title: '<span cl ...
- Easyui datagrid自定义排序
做项目遇到个关于排序问题,想着在前端排序,正好Easyui有这个功能,所以就拿来用了一下,因为跟官网的Demo不太一样,所以总结一下: 首先这一列是要排序的列(当然,在生产环境,这一列是隐藏的,在开发 ...
随机推荐
- api接口token验证
接口特点汇总: 1.因为是非开放性的,所以所有的接口都是封闭的,只对公司内部的产品有效: 2.因为是非开放性的,所以OAuth那套协议是行不通的,因为没有中间用户的授权过程: 3.有点接口需要用户登录 ...
- API接口签名验证2
http://www.jianshu.com/p/d47da77b6419 系统从外部获取数据时,通常采用API接口调用的方式来实现.请求方和�接口提供方之间的通信过程,有这几个问题需要考虑: 1.请 ...
- 重启nginx后丢失nginx.pid的解决方法
一,nginx的停止操作 停止操作是通过向nginx进程发送信号来实现的. 步骤1:查询nginx主进程号 复制代码 代码如下: ps -ef | grep nginx 在进程列表里 面找master ...
- PHPCMS模板里面使用自定义函数
首先我在/phpcms/libs/functions/extention.func.php里面新增了一个函数 ============================================= ...
- input标签元素,value属性取值问题,赋值
验证val:<input type="text" id="id" name="name" value="空值"&g ...
- android 基础02 - Activity 的生命周期及状态
返回栈 Android 中的 Activity 是可以层叠的,当我们启动一个新的 Activity 时,就会覆盖在原有的 Activity 之上, 点击 Back 会销毁当前 Activity,下面的 ...
- drawpoly()函数的用法
画多边形的函数drawpoly() 用当前绘图色.线型及线宽,画一个给定若干点所定义的多边形.第一个参数,是多边形的顶点数第二个参数,是该数组中是多边形所有顶点(x,y)坐标值,即一系列整数对
- 【编程技巧】java不使用第三个变量处理两个变量的交换
public class SwapNum {public static void main(String[] args) {doSwapNum();}public static void doSwap ...
- python实现冒泡排序和快速排序
冒泡排序和快排的python实现: data = [1, 3, 5, 10, 4, 7] times = 0 "冒泡排序" for i in range(len(data)): f ...
- CentOS 7安装Tomcat8
一.安装环境 tomcat的安装依赖于Java JDK,需要先安装配置正确的JDK http://www.cnblogs.com/VoiceOfDreams/p/8376978.html 二.安装包准 ...