WPF中拖动控件,实现位置随意摆放[2]
来源:互联网 发布:ip mac准入控制 编辑:程序博客网 时间:2024/04/30 13:28
在WPF中,除了可以通过处理鼠标事件来实现控件位置的拖动以外,还可以通过定义一些控件的行为,来实现控件的拖动,具体操作步骤如下:
自定义实现拖动的类库
1. 使用VS2010建立一个C#的类库
2. 增加"System.Windows.Interactivity.dll"库的引用\
如果使用的是Blend4,则位置为:"C:\Program Files\Microsoft SDKs\Expression\Blend\.NETFramework\v4.0\Libraries"
如果使用的是Blend3,则位置为:"C:\Program Files\Microsoft SDKs\Expression\Blend 3\Interactivity\Libraries\WPF"
3. 向项目中添加一个新的类文件.并命名为"DrapControlLibrary.cs",该文件的源码如下
using System;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Ink;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using System.Windows.Interactivity;namespace DrapControlLibrary{ public class DragInCanvasBehavior : Behavior<UIElement> { private Canvas canvas; protected override void OnAttached() { base.OnAttached(); // Hook up event handlers. this.AssociatedObject.MouseLeftButtonDown += AssociatedObject_MouseLeftButtonDown; this.AssociatedObject.MouseMove += AssociatedObject_MouseMove; this.AssociatedObject.MouseLeftButtonUp += AssociatedObject_MouseLeftButtonUp; } protected override void OnDetaching() { base.OnDetaching(); // Detach event handlers. this.AssociatedObject.MouseLeftButtonDown -= AssociatedObject_MouseLeftButtonDown; this.AssociatedObject.MouseMove -= AssociatedObject_MouseMove; this.AssociatedObject.MouseLeftButtonUp -= AssociatedObject_MouseLeftButtonUp; } // Keep track of when the element is being dragged. private bool isDragging = false; // When the element is clicked, record the exact position // where the click is made. private Point mouseOffset; private void AssociatedObject_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { // Find the canvas. if (canvas == null) canvas = VisualTreeHelper.GetParent(this.AssociatedObject) as Canvas; // Dragging mode begins. isDragging = true; // Get the position of the click relative to the element // (so the top-left corner of the element is (0,0). mouseOffset = e.GetPosition(AssociatedObject); // Capture the mouse. This way you'll keep receiveing // the MouseMove event even if the user jerks the mouse // off the element. AssociatedObject.CaptureMouse(); } private void AssociatedObject_MouseMove(object sender, MouseEventArgs e) { if (isDragging) { // Get the position of the element relative to the Canvas. Point point = e.GetPosition(canvas); // Move the element. AssociatedObject.SetValue(Canvas.TopProperty, point.Y - mouseOffset.Y); AssociatedObject.SetValue(Canvas.LeftProperty, point.X - mouseOffset.X); } } private void AssociatedObject_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (isDragging) { AssociatedObject.ReleaseMouseCapture(); isDragging = false; } } }}
4. 编译生成类库文件,类库文件名称为"DrapControlLibrary.dll"
制作测试程序
1. 使用Blend4新建一个WPF程序
2. 修改主XAML文件,修改后的结果如下
<Windowxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:custom="clr-namespace:DrapControlLibrary;assembly=DrapControlLibrary" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"x:Class="DrapControlTest.MainWindow"x:Name="Window"Title="DrapControlTest"Width="300" Height="250" ResizeMode="CanMinimize" Background="White"> <Canvas> <TreeView Canvas.Left="10" Canvas.Top="80" Width="200" Height="50"> <i:Interaction.Behaviors> <custom:DragInCanvasBehavior/> </i:Interaction.Behaviors> </TreeView> <ListView Canvas.Left="10" Canvas.Top="15" Width="200" Height="50"> <i:Interaction.Behaviors> <custom:DragInCanvasBehavior/> </i:Interaction.Behaviors> </ListView> <Rectangle Canvas.Left="10" Canvas.Top="10" Fill="Yellow" Width="40" Height="60"/> <Ellipse Canvas.Left="10" Canvas.Top="70" Fill="Blue" Width="80" Height="60"> <i:Interaction.Behaviors> <custom:DragInCanvasBehavior/> </i:Interaction.Behaviors> </Ellipse> <Ellipse Canvas.Left="80" Canvas.Top="70" Fill="OrangeRed" Width="40" Height="70"> <i:Interaction.Behaviors> <custom:DragInCanvasBehavior/> </i:Interaction.Behaviors> </Ellipse> </Canvas></Window>
3. 编译文件,能够看到效果了吧?是不是很帅
- WPF中拖动控件,实现位置随意摆放[2]
- WPF中拖动控件,实现位置随意摆放[1]
- WPF中拖动控件,实现位置随意摆放
- WPF实现控件拖动
- .net 2003 项目中关于分页与随意摆放控件
- Android 控件随意拖动
- Android自定义控件实现手机摆放位置的校准
- WPF下实现控件的拖动操作
- 【转】android 怎么随意摆放button的位置?
- c# WPF 中图片缩放功能,鼠标拖动位置
- JavaSwing中随意拖动组件
- WPF中动态改变控件显示位置
- 关于相对布局中控件摆放位置的一个需要注意的小地方
- 随意拖动的picturebox服务器控件源码
- 在WPF中的Canvas上实现控件的拖动、缩放
- [.Net码农].NET运行界面上,实现随意拖动控件的方法
- WPF Popup实现拖动
- QT中在界面中任何拖动控件位置
- WPF中拖动控件,实现位置随意摆放[1]
- AVC/H264编码教程
- 函数指针和指针函数以及函数指针数组
- JNDI到底是什么,有什么作用
- CentOS防火墙开启、关闭以及开放指定端口
- WPF中拖动控件,实现位置随意摆放[2]
- 元素上下移动
- java学习——如何实现线程之间的通信 ,Condition 的使用
- route-map实现简单的策略路由+浮动静态路由的概念!
- YII Framework学习教程-YII的分页CPagination-2011-11-23
- CSDN牛人博客!随时更新!...
- js的多项选择
- HTTP转发到HTTS
- Layer层属性,要设置头文件