原文: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操作的更多相关文章

  1. HTML 学习笔记 (drag & drop)

    拖放(Drag & Drop)是一种常见的特性,即抓取对象以后拖到另一个位置.在 HTML5 中,拖放是标准的一部分,任何元素都能够拖放.过去,我们用监听鼠标的Mousedown.Mouseo ...

  2. HTML5魔法堂:全面理解Drag & Drop API

    一.前言    在HTML4的时代,各前端工程师为了实现拖拽功能可说是煞费苦心,初听HTML5的DnD API觉得那些痛苦的日子将一去不复返,但事实又是怎样的呢?下面我们一起来看看DnD API的真面 ...

  3. Win10/UWP新特性—Drag&Drop 拖出元素到其他App

    在以前的文章中,写过微软新特性Drag&Drop,当时可能由于处于Win10预览版,使用的VS也是预览版,只实现了从桌面拖拽文件到UWP App中,没能实现从UWP拖拽元素到Desktop A ...

  4. JS魔法堂:IE5~9的Drag&Drop API

    一.前言     < HTML5魔法堂:全面理解Drag & Drop API>中提到从IE5开始已经支持DnD API,但IE5~9与HTML5的API有所不同,下面我们来了解一 ...

  5. [转]人人网首页拖拽上传详解(HTML5 Drag&Drop、FileReader API、formdata)

    人人网首页拖拽上传详解(HTML5 Drag&Drop.FileReader API.formdata) 2011年12月11日 | 彬Go 上一篇:给力的 Google HTML5 训练营( ...

  6. Android drag drop

    最近偶尔知道了锤子的one step,所以在网上看相关的东西,有人说android原生drag drop就能实现,我就去学习一下这个drag drop,下面把学习到的东西总结一下: drag drop ...

  7. 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 注 ...

  8. Atitit。D&D drag&drop拖拽功能c#.net java swing的对比与实现总结

    Atitit.D&D drag&drop拖拽功能c#.net java swing的对比与实现总结 1. 实现一个D&D操作一般包括三个步骤: 1 2. .net黑头的拖曳机制 ...

  9. HTML5中的拖拽与拖放(drag&&drop)

    1.drag 当拖动某个元素时,将会依次触发下列事件: 1)dragstart:按下鼠标键并开始移动鼠标时,会触发该事件 2)drag:dragstart触发后,随即便触发drag事件,而且在元素被拖 ...

随机推荐

  1. Android 防止切换横屏闪退

    <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="ht ...

  2. Greenplum(PostgreSql)函数实现批量删除表

    项目做库迁移,前期需要经常调整表结构语句,涉及多次的批量drop,本着偷懒精神写了这个函数.鉴于本函数在生产环境有巨大风险,建议测试完毕后立即删除. 主要步骤很简单:1)从pg_tables查询得到相 ...

  3. Mybatis笔记 - Mapper动态代理

    使用Mybatis开发Dao,通常有两个方法,即原始Dao开发方法和Mapper接口开发方法. Mapper接口开发方式是基于入门程序的基础上,对 控制程序 进行分层开发,程序员只需要 编写mappe ...

  4. (转)poi操作Excel, 各种具体操作和解释

    原文地址http://hi.baidu.com/j_changhong/item/981fa58d05fa755926ebd96b注原文是3.6 此文是3.9 java读取excel文件的顺序是: E ...

  5. (转)python之函数介绍及使用

    为什么要用函数? 1.减少代码量 2.保持一致性 3.易维护 一.函数的定义和使用 ? 1 2 3 4 5 6 def test(参数):              ...     函数体     . ...

  6. RoHS

    RoHS是<电气.电子设备中限制使用某些有害物质指令>(the Restriction of the use of certain hazardous substances in elec ...

  7. css3 常用的属性

    1.伪类选择符 Pseudo-Classes Selectors Selectors选择符 CSS Version版本 Description简介 E:link CSS1 设置超链接a在未被访问前的样 ...

  8. Ctrl快捷键

    Ctrl + a - Jump to the start of the lineCtrl + b - Move back a charCtrl + c - Terminate the command ...

  9. Linux上 安装Sorl4.7 中间件用tomcat

    最近需要用到solr,公司内部搭建了一个solr测试环境. 版本:solr4.7.2 ,tomcat 7.0.55 jdk:1.7_051 解压 solr 和tomcat  这里就不详说. 1.启动t ...

  10. Codeforces 479【C】div3

    题目链接:http://codeforces.com/problemset/problem/977/C 题意:给你n个数字,输出任意一个数字,这个数字刚好大于等于,序列里面k个数字. 题解:排个序,第 ...