WPF控件拖动
支持任意方向拖动/水平方向拖动/垂直方向拖动
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | public enum DragDirection { [Description( "任意" )] Any, [Description( "水平" )] Horizontal, [Description( "垂直" )] Vertical, } public class ControlDragExtends : DependencyObject { public static int GetMaxX(DependencyObject obj) { return ( int )obj.GetValue(MaxXProperty); } public static void SetMaxX(DependencyObject obj, int value) { obj.SetValue(MaxXProperty, value); } public static int GetMaxY(DependencyObject obj) { return ( int )obj.GetValue(MaxYProperty); } public static void SetMaxY(DependencyObject obj, int value) { obj.SetValue(MaxYProperty, value); } public static int GetMinX(DependencyObject obj) { return ( int )obj.GetValue(MinXProperty); } public static void SetMinX(DependencyObject obj, int value) { obj.SetValue(MinXProperty, value); } public static int GetMinY(DependencyObject obj) { return ( int )obj.GetValue(MinYProperty); } public static void SetMinY(DependencyObject obj, int value) { obj.SetValue(MinYProperty, value); } public static DragDirection GetDragDirection(DependencyObject obj) { return (DragDirection)obj.GetValue(DragDirectionProperty); } public static void SetDragDirection(DependencyObject obj, DragDirection value) { obj.SetValue(DragDirectionProperty, value); } public static bool GetEnableDragMove(DependencyObject obj) { return ( bool )obj.GetValue(EnableDragMoveProperty); } public static void SetEnableDragMove(DependencyObject obj, bool value) { obj.SetValue(EnableDragMoveProperty, value); } // Using a DependencyProperty as the backing store for DragDirection. This enables animation, styling, binding, etc... public static readonly DependencyProperty MaxYProperty = DependencyProperty.Register( "MaxY" , typeof ( int ), typeof (ControlDragExtends), new PropertyMetadata( int .MaxValue)); // Using a DependencyProperty as the backing store for DragDirection. This enables animation, styling, binding, etc... public static readonly DependencyProperty MaxXProperty = DependencyProperty.Register( "MaxX" , typeof ( int ), typeof (ControlDragExtends), new PropertyMetadata( int .MaxValue)); // Using a DependencyProperty as the backing store for DragDirection. This enables animation, styling, binding, etc... public static readonly DependencyProperty MinYProperty = DependencyProperty.Register( "MinY" , typeof ( int ), typeof (ControlDragExtends), new PropertyMetadata(0)); // Using a DependencyProperty as the backing store for DragDirection. This enables animation, styling, binding, etc... public static readonly DependencyProperty MinXProperty = DependencyProperty.Register( "MinX" , typeof ( int ), typeof (ControlDragExtends), new PropertyMetadata(0)); // Using a DependencyProperty as the backing store for DragDirection. This enables animation, styling, binding, etc... public static readonly DependencyProperty DragDirectionProperty = DependencyProperty.Register( "DragDirection" , typeof (DragDirection), typeof (ControlDragExtends), new PropertyMetadata(DragDirection.Any)); /// <summary> /// 回车/空格 /// </summary> public static readonly DependencyProperty EnableDragMoveProperty = DependencyProperty.RegisterAttached( "EnableDragMove" , typeof ( bool ), typeof (ControlDragExtends), new PropertyMetadata( false , OnEnableDragMove)); private static void OnEnableDragMove(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (e.NewValue != null ) { var element = d as FrameworkElement; if (element == null ) return ; if (( bool )e.NewValue) { element.PreviewMouseDown += Element_PreviewMouseDown; element.PreviewMouseMove += Element_PreviewMouseMove; element.PreviewMouseUp += Element_PreviewMouseUp; } } } private static void Element_PreviewMouseDown( object sender, MouseButtonEventArgs e) { var c = sender as Control; _isMouseDown = true ; _mouseDownPosition = e.GetPosition( null ); Console.WriteLine(_mouseDownPosition); var transform = c.RenderTransform as TranslateTransform; if (transform == null ) { transform = new TranslateTransform(); c.RenderTransform = transform; } _mouseDownControlPosition = new Point(transform.X, transform.Y); c.CaptureMouse(); } //鼠标是否按下 static bool _isMouseDown = false ; //鼠标按下的位置 static Point _mouseDownPosition; //鼠标按下控件的位置 static Point _mouseDownControlPosition; private static void Element_PreviewMouseMove( object sender, MouseEventArgs e) { if (_isMouseDown) { var element = sender as FrameworkElement; if (element == null ) return ; var transform = element.RenderTransform as TranslateTransform; if (transform == null ) return ; var pos = e.GetPosition( null ); var dp = pos - _mouseDownPosition; var newX = _mouseDownControlPosition.X + dp.X; var newY = _mouseDownControlPosition.Y + dp.Y; switch (GetDragDirection(element)) { case DragDirection.Horizontal: newX = Math.Max(GetMinX(element), newX); newX = Math.Min(GetMaxX(element), newX); newY = transform.Y; break ; case DragDirection.Vertical: newY = Math.Max(GetMinY(element), newY); newY = Math.Min(GetMaxY(element), newY); newX = transform.X; break ; case DragDirection.Any: default : break ; } transform.X = newX; transform.Y = newY; } } private static void Element_PreviewMouseUp( object sender, MouseButtonEventArgs e) { var c = sender as Control; _isMouseDown = false ; c.ReleaseMouseCapture(); } } |
使用
1 2 3 4 5 6 7 | <Grid> <Button HorizontalAlignment= "Left" Margin= "20" Content= "拖动我" Width= "100" Height= "45" local:ControlDragExtends.EnableDragMove= "True" ></Button> <Button x:Name= "ButtonTest" local:ControlDragExtends.EnableDragMove= "True" local:ControlDragExtends.DragDirection= "Vertical" local:ControlDragExtends.MaxY= "100" HorizontalAlignment= "Left" VerticalAlignment= "Top" Margin= "20,20" Content= "附加拖动" Width= "100" Height= "45" ></Button> <Button x:Name= "ButtonTestMaxX" local:ControlDragExtends.EnableDragMove= "True" local:ControlDragExtends.DragDirection= "Horizontal" local:ControlDragExtends.MaxX= "500" local:ControlDragExtends.MinX= "-1000" HorizontalAlignment= "Right" VerticalAlignment= "Top" Margin= "20,20" Content= "附加拖动" Width= "100" Height= "45" ></Button> </Grid> |
效果
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?