WPF实现Drag/Drop操作
有时候我们方便用户操作,总会把一下Copy/Paste 或者 input操作转换为Drag/Drop, WPF 跟之前WinForm 一样提供了一些实现方式方便开发人员进行开发。
要允许控件Drop操作,首先必须设置控件属性AllowDrop,这样控件才能产生DragOver/DragEnter/Drop等相关事件,从而开发人员可以做一些逻辑判断,设置DragEventArgs 的属性Effects 决定Drag的内容是否允许Drop. 下面是一个简单的例子:
创建两个ListBox, 一个里面有内容(名字为:EmployeeSource),另外一个(名字:SelectedEmployees)没有内容,允许用户从EmployeeSource 拖拽内容到SelectedEmployees。
1. 在Xaml中创建这两个ListBox, 并进行相关数据绑定
<ListBox x:Name="SelectedEmployees" HorizontalAlignment="Left" Height="100" Margin="35,30,0,0"
ItemsSource="{Binding SelectedEmployeeData}" DisplayMemberPath="Description"
VerticalAlignment="Top" Width="275" AllowDrop="True"
Drop="SelectedEmployees_Drop" DragOver="SelectedEmployees_DragOver" DragLeave="SelectedEmployees_DragLeave"
MouseMove="SelectedEmployees_MouseMove"/>
<ListBox x:Name="EmployeeSource" DataContext="{Binding Source={StaticResource EmployeeList}}"
SelectionMode="Single" HorizontalAlignment="Left" Height="100" Margin="35,255,0,0" VerticalAlignment="Top" Width="280"
ItemsSource="{Binding Employees}" DisplayMemberPath="Description" MouseMove="EmployeeSource_MouseMove"/>
2. 在cs文件中实现相关事件函数
bool bMouseDown = true;
private void EmployeeSource_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
// TODO: Add event handler implementation here.
bMouseDown = e.LeftButton == MouseButtonState.Pressed;
if (EmployeeSource.SelectedItems.Count == 0 || !bMouseDown)
return;
ICollection<EmployeesItem> selected = new List<EmployeesItem>();
foreach(var item in EmployeeSource.SelectedItems)
{
var employee = item as EmployeesItem;
if (employee == null)
return;
selected.Add(employee);
}
DragDrop.DoDragDrop(EmployeeSource, selected, DragDropEffects.Copy);
}
private void SelectedEmployees_Drop(object sender, System.Windows.DragEventArgs e)
{
// TODO: Add event handler implementation here.
var ems = e.Data.GetData(typeof(List<EmployeesItem>)) as List<EmployeesItem>;
if (ems == null)
return;
foreach(var em in ems)
{
SelectedEmployeeData.Add(em);
}
}
private void SelectedEmployees_DragOver(object sender, System.Windows.DragEventArgs e)
{
// TODO: Add event handler implementation here.
var ems = e.Data.GetData(typeof(List<EmployeesItem>)) as List<EmployeesItem>;
if (ems == null)
{
e.Effects = DragDropEffects.None;
return;
}
DragElement = ems[0];
e.Effects = DragDropEffects.Copy;
}
3. 增加DataContext(ViewModel), 设置绑定的属性
public class EmployeeList : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public EmployeeList()
{
try
{
Uri resourceUri = new Uri("/WPFStudy;component/SampleData/EmployeeList/EmployeeList.xaml", UriKind.RelativeOrAbsolute);
System.Windows.Application.LoadComponent(this, resourceUri);
}
catch
{
}
}
private Employees _Employees = new Employees();
public Employees Employees
{
get
{
return this._Employees;
}
}
}
public class EmployeesItem : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public override bool Equals(object obj){var tmp = obj as EmployeesItem;if (tmp == null) return false;return tmp.Description.Equals(Description);}
private string _Description = string.Empty;
public string Description
{
get
{
return this._Description;
}
set
{
if (this._Description != value)
{
this._Description = value;
this.OnPropertyChanged("Description");
}
}
}
}
public class Employees : System.Collections.ObjectModel.ObservableCollection<EmployeesItem>
{
}
WPF实现Drag/Drop操作的更多相关文章
- HTML 学习笔记 (drag & drop)
拖放(Drag & Drop)是一种常见的特性,即抓取对象以后拖到另一个位置.在 HTML5 中,拖放是标准的一部分,任何元素都能够拖放.过去,我们用监听鼠标的Mousedown.Mouseo ...
- HTML5魔法堂:全面理解Drag & Drop API
一.前言 在HTML4的时代,各前端工程师为了实现拖拽功能可说是煞费苦心,初听HTML5的DnD API觉得那些痛苦的日子将一去不复返,但事实又是怎样的呢?下面我们一起来看看DnD API的真面 ...
- Win10/UWP新特性—Drag&Drop 拖出元素到其他App
在以前的文章中,写过微软新特性Drag&Drop,当时可能由于处于Win10预览版,使用的VS也是预览版,只实现了从桌面拖拽文件到UWP App中,没能实现从UWP拖拽元素到Desktop A ...
- JS魔法堂:IE5~9的Drag&Drop API
一.前言 < HTML5魔法堂:全面理解Drag & Drop API>中提到从IE5开始已经支持DnD API,但IE5~9与HTML5的API有所不同,下面我们来了解一 ...
- [转]人人网首页拖拽上传详解(HTML5 Drag&Drop、FileReader API、formdata)
人人网首页拖拽上传详解(HTML5 Drag&Drop.FileReader API.formdata) 2011年12月11日 | 彬Go 上一篇:给力的 Google HTML5 训练营( ...
- Android drag drop
最近偶尔知道了锤子的one step,所以在网上看相关的东西,有人说android原生drag drop就能实现,我就去学习一下这个drag drop,下面把学习到的东西总结一下: drag drop ...
- atitit.D&D drag&drop拖拽文件到界面功能 html5 web 跟个java swing c#.net c++ 的总结
atitit.D&D drag&drop拖拽文件到界面功能 html5 web 跟个java swing c#.net c++ 的总结 1. DND的操作流程 1 2. Html5 注 ...
- Atitit。D&D drag&drop拖拽功能c#.net java swing的对比与实现总结
Atitit.D&D drag&drop拖拽功能c#.net java swing的对比与实现总结 1. 实现一个D&D操作一般包括三个步骤: 1 2. .net黑头的拖曳机制 ...
- HTML5中的拖拽与拖放(drag&&drop)
1.drag 当拖动某个元素时,将会依次触发下列事件: 1)dragstart:按下鼠标键并开始移动鼠标时,会触发该事件 2)drag:dragstart触发后,随即便触发drag事件,而且在元素被拖 ...
随机推荐
- JAVA java
{ 用法: java [-options] class [args...] (执行类) 或 java [-options] -jar jarfile [args...] ...
- magento 跳转
Magento: Redirect functions 原文:http://blog.chapagain.com.np/magento-redirect-functions/ The redirect ...
- R语言 变量
R语言变量 变量为我们提供了我们的程序可以操作的命名存储. R语言中的变量可以存储原子向量,原子向量组或许多Robject的组合. 有效的变量名称由字母,数字和点或下划线字符组成. 变量名以字母或不以 ...
- Cucumber:启动类配置
Cucumber:启动类配置 package com.steps; import cucumber.api.CucumberOptions; import cucumber.api.junit.Cuc ...
- tarjan强连通分量 (模板)
#include<iostream> #include<cstdio> using namespace std; const int MAXN = 10005; struct ...
- NX二次开发-比较两个string是否相等
NX11+VS2013 #include <uf.h> #include <uf_ui.h> UF_initialize(); string A = "ABC&quo ...
- ArcGIS中QueryTask,FindTask,IndentifyTask 之间的区别
1:QueryTask是一个进行空间和属性查询的功能类,它可以在某个地图服务的某个子图层内进行查询,顺便需要提一下的是,QueryTask进行查询的地图服务并 不必项加载到Map中进行显示.Query ...
- 报错:[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the paren
今天在做Vue的时候,子组件关闭的时候,报如下错误 报错:vue.esm.js?65d7:610 [Vue warn]: Avoid mutating a prop directly since th ...
- C++实现的B树
参考资料:按第一个参考资料构建,代码基本上来自于第二个参考资料 https://www.cnblogs.com/guohai-stronger/p/9225057.html https://www.c ...
- Codeforces 1166B - All the Vowels Please
题目链接:http://codeforces.com/problemset/problem/1166/B 个元音. 思路:先判断能否弄出至少5*5的行列,然后按顺序填字符串就好了. AC代码: #in ...