WPF DataGrid Drag
来源:互联网 发布:淘宝在哪改收货地址 编辑:程序博客网 时间:2024/05/29 09:37
DataGrid里的拖动排序 转:http://www.cnblogs.com/yhnbgfd/p/3792806.html
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 = 1, Name = "N1" });
list1.Add(new Model_DataGrid { Id = 2, Name = "N2" });
list1.Add(new Model_DataGrid { Id = 3, Name = "N3" });
list1.Add(new Model_DataGrid { Id = 4, Name = "N4" });
list1.Add(new Model_DataGrid { Id = 5, Name = "N5" });
list1.Add(new Model_DataGrid { Id = 6, Name = "N6" });
list1.Add(new Model_DataGrid { Id = 7, Name = "N7" });
list1.Add(new Model_DataGrid { Id = 8, Name = "N8" });
list1.Add(new Model_DataGrid { Id = 9, Name = "N9" });
this.DataGrid1.ItemsSource = list1;
}
private void InitializeDataGrid2()
{
list2 = new ObservableCollection<Model_DataGrid>();
list2.Add(new Model_DataGrid { Id = 1, Name = "Na1" });
list2.Add(new Model_DataGrid { Id = 2, Name = "Na2" });
list2.Add(new Model_DataGrid { Id = 3, Name = "Na3" });
list2.Add(new Model_DataGrid { Id = 4, Name = "Na4" });
list2.Add(new Model_DataGrid { Id = 5, Name = "Na5" });
list2.Add(new Model_DataGrid { Id = 6, 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 > 0)
{
Model_DataGrid DragData = this.DataGrid1.SelectedCells[0].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
- wpf datagrid
- WPF DataGrid
- Drag and Drop in WPF
- WPF实现Drag/Drop操作
- WPF drag过程中显示ToolTip.
- WPF WebBrowser DataGrid
- wpf datagrid 详细介绍
- wpf button ,datagrid style
- WPF DataGrid Control
- wpf datagrid 数据绑定
- printing DataGrid in WPF
- wpf datagrid 加入checkbox
- WPF DataGrid 绑定Table
- WPF之DataGrid应用
- WPF Datagrid Tooltip
- wpf DataGrid 虚拟化
- WPF分页DataGrid
- HDU1202
- 取出两个数组不相同的元素并返回到新的数组里面
- Linux下的tar压缩解压缩命令详解
- 23种设计模式(22):组合模式
- Java I/O
- WPF DataGrid Drag
- html5手机开发cookie的用法总结
- .net mvc4 一个 view 显示多个 model
- 大数据应用统一集成平台CDAP简介
- HDU1205
- 二进制的转换
- 如何判断IP地址是否合法
- 尴尬了!OLED屏幕还未普及开来,人们就开始讨论起Micro-LED了!
- Linux安装Tomcat