实现了WPF的不同层级间的元素之间的拖放,例子虽小却很经典,引申一下也许可以实现类VS界面的浮动依靠面板。

拖放前:

拖放后:

代码如下:

<Window x:Class="WpfApplication8.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="" Width="">
<Grid>
<Canvas Margin="0,0,216,0" Name="canvas1" Background="Crimson" AllowDrop="True" Drop="C1Drop">
<Rectangle Height="" Margin="10,10" Name="rectangle1" Width="" Fill="Azure" PreviewMouseMove="RectMove" />
</Canvas>
<Canvas HorizontalAlignment="Right" Margin="0,0,0,0" Name="canvas2"
Width="" Background="DarkSalmon" AllowDrop="True" Drop="C2Drop"/>
</Grid>
</Window>
        private void C1Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(Rectangle)))
{
string str = ((Canvas)(this.rectangle1.Parent)).Name.ToString();
if(str.Equals("canvas2"))
{
Rectangle rect = (Rectangle)e.Data.GetData(typeof(Rectangle));
this.canvas2.Children.Remove(rect);
this.canvas1.Children.Add(rect);
} }
}
private void C2Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(Rectangle)))
{
string str = ((Canvas)(this.rectangle1.Parent)).Name.ToString();
if (str.Equals("canvas1"))
{
Rectangle rect = (Rectangle)e.Data.GetData(typeof(Rectangle));
this.canvas1.Children.Remove(rect);
this.canvas2.Children.Add(rect);
}
}
}
private void RectMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed) // 如果鼠标左键被按下
{
DataObject data = new DataObject(typeof(Rectangle), this.rectangle1);
DragDrop.DoDragDrop(this.rectangle1, data, DragDropEffects.Move);
}
}

感觉博主苏扬的分享:http://msdn.microsoft.com/zh-cn/ff685657.aspx

最新文章

  1. Spring:源码解读Spring IOC原理
  2. Moon.Orm常见问题问答FAQ
  3. 基本shell编程【2】-服务端发布脚本
  4. bzoj1724: [Usaco2006 Nov]Fence Repair 切割木板
  5. sqlite数据类型
  6. MySQL中的datetime与timestamp比较-------转载
  7. 学习java之HashMap和TreeMap
  8. 省时的浏览器同步测试工具 browsersync NodeJS
  9. 【学习笔记】【C语言】循环结构-do while
  10. python:UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xef in position xxx: ordinal not in range(128)
  11. gallery利用代码定位图片并且不丢失动画效果
  12. Java乔晓松-android的四大组件之一Service(服务的绑定)
  13. ZOJ2185 简单分块 找规律
  14. python 面对post分页爬虫
  15. Android Studio 3.1 Beta 1发布,如何及时下载更新
  16. 金三银四跳槽季,BAT美团滴滴java面试大纲(带答案版)之一:Java基础篇
  17. Vue源码
  18. Quidway S系列交换机
  19. open read split
  20. C 应用

热门文章

  1. Python脚本打包为exe文件
  2. 【TMF eTOM】业务流程框架介绍
  3. 【ssm整合打印sql语句】
  4. xsp4 命令行配置运行(CLI工具)
  5. 在Android中使用FlatBuffers(下篇)
  6. sql server 2008 r2安装
  7. Java中Class Type 类类型是怎么回事?
  8. CodeForces 106C 【DP】
  9. java整理(一)
  10. springmvc json 简单例子