当前位置:网站首页>【wpf】拖拽的简单实现
【wpf】拖拽的简单实现
2022-08-10 00:54:00 【code bean】
前台代码部分
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<StackPanel Background="AliceBlue">
<Border Height="40" Background="Chocolate" MouseLeftButtonDown="Border_MouseLeftButtonDown">
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">拖拽</TextBlock>
</Border>
</StackPanel>
<Canvas x:Name="canvas" Grid.Column="1" Drop="Canvas_Drop" AllowDrop="True" Background="Transparent"/>
</Grid>
这里有个几个注意的地方:
1 cavar是拖拽的放置对象,需要设置如下:
- Drop事件,拖拽完成时触发
- AllowDrop, 允许拖拽
- Background,设置一个颜色,不然无法接收到事件。
三者缺一不可。
后台代码部分
Border 是被拖拽的对象,在鼠标左键按下后,需要在后台添加一些代码。
private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Border border = (Border)sender;
DragDrop.DoDragDrop(border, "test", DragDropEffects.Copy);
}
这是一个创建拖拽对象的过程。 后续可以在拖拽事件中获取相应的参数:
private void Canvas_Drop(object sender, DragEventArgs e)
{
var data = e.Data.GetData(typeof(string));
Button btn = new Button();
btn.Content = data;
canvas.Children.Add(btn);
var point = e.GetPosition(canvas);
Canvas.SetLeft(btn, point.X);
Canvas.SetTop(btn, point.Y);
Debug.WriteLine("Canvas_Drop");
}
e.Data.GetData(typeof(string)); 获取的是我创建拖拽对象时,传入的第二个参数。
e.GetPosition(canvas); 可以获取拖拽完成时,鼠标相对于canvas的位置坐标!
效果展示
边栏推荐
- Docker interview question 2--get the number of database connections and docker-compose
- Unity image is blurry after using long image
- 【Grpc】简介
- Initial attempt at UI traversal
- 墨西哥大众VW Mexico常见的几种label
- 【Swoole系列3.5】进程池与进程管理器
- SonarQube升级记录:7.8->7.9->8.9
- 【CAS:41994-02-9 |Biotinyl tyramide】Biotinyl tyramide price
- Summary of basic operations of c language files
- -red and black-
猜你喜欢
Mysql database ALTER basic operations
【kali-密码攻击】(5.2.1)密码分析:Hash Identifier(哈希识别)
开发IM即时通讯容易吗?需要什么技术
基于设计稿识别的可视化低代码系统实践
Research on Ethernet PHY Chip LAN8720A Chip
手把手教你编写性能测试用例
y92.第六章 微服务、服务网格及Envoy实战 -- Envoy基础(三)
D-Biotinol Involved by Biotin, CAS No: 53906-36-8 Specific Properties Description
将string类对象中的内容格式化到字符串Buffer中时遇到的异常崩溃分析
高校就业管理系统设计与实现
随机推荐
3511. 倒水问题
Research on Ethernet PHY Chip LAN8720A Chip
无js实现弹出层效果
多线程之自定义线程池
shell指定参数名传参
基于FTP协议实现文件上传与下载
03|Process Control
Solve the problem of sed replacement text containing special characters such as "/" and "#"
芯片资讯|半导体收入增长预计将放缓至 7%,蓝牙芯片需求依然稳步增长
Docker interview question 2--get the number of database connections and docker-compose
基于设计稿识别的可视化低代码系统实践
头脑风暴:单词拆分
ITK编译remote库
What do you know about FITC-labeled biotin (FITC-biotin|CAS: 134759-22-1)?
Data storage - the C language
pyhton之问~~~~~if __name__ == ‘__main__‘:是什么?
递归 二分查找 冒泡排序 快速排序
C language structure, function and pointer exercise (simple address book)
unity编辑器扩展界面使用 List
墨西哥大众VW Mexico常见的几种label