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