Silverlight拖放封装

本文介绍了一个用于WPF应用程序的实用扩展方法,该方法允许开发者轻松地为控件绑定拖动行为,并限制其在指定边界内移动。通过简单的代码示例展示了如何实现这一功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public static class ExtendMethords
    {
        /// <summary>
        /// 核心拖动扩展方法
        /// </summary>
        /// <param name="dragTarget">被拖动的对象</param>
        /// <param name="dragElement">可用鼠标拖拽的部分</param>
        /// <param name="bound">被拖动对象可移动的范围</param>
        /// <returns></returns>
        public static bool BindDrag(this FrameworkElement dragTarget, FrameworkElement dragElement, DragBound bound)
        {
            bool result = false;
            if (dragTarget.Parent != null && (dragTarget.Parent as Canvas) != null)
            {
                Canvas moveCvs = dragTarget.Parent as Canvas;
                Point downPosition = new Point();
                Point ParentPosition = new Point();
                bool isDrag = false;
                DragBoundCheck check = new DragBoundCheck(dragTarget, bound);
                dragElement.MouseLeftButtonDown += delegate(object sender, MouseButtonEventArgs e)
                {
                    IDrag dragObj = dragTarget as IDrag;
                    if (dragObj != null && dragObj.IsDrag == false)
                    {
                        return;
                    }
                    if (dragElement.CaptureMouse())
                    {
                        downPosition = e.GetPosition(dragElement);
                        Point mouseOnParentPosition = e.GetPosition(moveCvs);
                        Point mouseOnSlPosition = e.GetPosition(null);
                        ParentPosition = mouseOnSlPosition.Sub(mouseOnParentPosition);
                        isDrag = true;
                    }
                };
                dragElement.MouseMove += delegate(object sender, MouseEventArgs e)
                {
                    if (isDrag)
                    {
                        Point mouseOnSlPosition = e.GetPosition(null);
                        Point targetOnSlPosition = mouseOnSlPosition.Sub(downPosition);
                        targetOnSlPosition = check.GetInBoundPosition(targetOnSlPosition);
                        Point targetOnParentPosition = targetOnSlPosition.Sub(ParentPosition);
                        dragTarget.SetPosition(targetOnParentPosition);

                    }
                };
                dragElement.MouseLeftButtonUp += delegate(object sender, MouseButtonEventArgs e)
                {
                    if (isDrag)
                    {
                        isDrag = false;
                        dragElement.ReleaseMouseCapture();
                    }
                };
                result = true;
            }
            return result;
        }
        /// <summary>
        /// 拖动扩展方法,可以被到处拖动
        /// </summary>
        /// <param name="dragTarget">被拖动的对象</param>
        /// <param name="dragElement">用鼠标拖拽的部分</param>
        /// <returns></returns>
        public static bool BindDrag(this FrameworkElement dragTarget, FrameworkElement dragElement)
        {
            DragBound bound = new DragBound();
            return BindDrag(dragTarget, dragElement, bound);
        }
        /// <summary>
        /// 拖动扩展方法,可用鼠标拖拽的部分就是被拖动对象本身
        /// </summary>
        /// <param name="dragTarget"></param>
        /// <param name="bound"></param>
        /// <returns></returns>
        public static bool BindDrag(this FrameworkElement dragTarget, DragBound bound)
        {

            return BindDrag(dragTarget, dragTarget, bound);
        }
        /// <summary>
        /// 没有任何限定的拖动方法
        /// </summary>
        /// <param name="dragTarget"></param>
        /// <returns></returns>
        public static bool BindDrag(this FrameworkElement dragTarget)
        {
            DragBound bound = new DragBound();
            return BindDrag(dragTarget, bound);
        }

         //一下几个函数比较简单,大家一看就明白,就不写注释了,呵呵。
        public static void SetLeft(this FrameworkElement element,double leftPosition)
        {
            element.SetValue(Canvas.LeftProperty, leftPosition);
        }
        public static void SetTop(this FrameworkElement element, double topPosition)
        {
            element.SetValue(Canvas.TopProperty, topPosition);
        }
        public static void SetPosition(this FrameworkElement element, Point position)
        {
            element.SetLeft(position.X);
            element.SetTop(position.Y);
        }

        public static Point Sub(this Point selfPoint, Point subPoint)
        {
            Point result = new Point(selfPoint.X - subPoint.X, selfPoint.Y - subPoint.Y);
            return result;
        }
        public static Point Add(this Point selfPoint, Point addPoint)
        {
            Point result = new Point(selfPoint.X + addPoint.X, selfPoint.Y + addPoint.Y);
            return result;
        }
    
    }

      //函数里面有这么几个类,逻辑也很简单,就直接贴在这里了
      

      public interface IDrag
    {
        bool IsDrag { set; get; }
    }
    /// <summary>
    /// 检测位置是否在设定的范围内
    /// </summary>
    internal class DragBoundCheck
    {
        private FrameworkElement element;
        private DragBound bound;
        public DragBoundCheck(FrameworkElement element,DragBound bound)
        {
                this.element = element;
                this.bound = bound;
        }

        public Point GetInBoundPosition(Point newPosition)
        {
         
            if (newPosition.X > bound.Right-element.ActualWidth)
            {
                newPosition.X = bound.Right - element.ActualWidth;
            }
            if (newPosition.X < bound.Left)
            {
                newPosition.X = bound.Left;
            }
            if (newPosition.Y < bound.Top)
            {
                newPosition.Y = bound.Top;
            }
            if (newPosition.Y > bound.Bottom-element.ActualHeight)
            {
                newPosition.Y = bound.Bottom - element.ActualHeight;
            }
            return newPosition;
        }
    }
    /// <summary>
    ///  设定拖动的范围
    /// </summary>
    public class DragBound
    {
        public double Top { set; get; }
        public double Bottom
        {
            set;
            get;
        }
        public double Left { set; get; }
        public double Right
        {
            set;
            get;
        }
        public DragBound()
        {
            Top = 0.0;
            Left = 0.0;
            //Right = (double)System.Windows.Browser.HtmlPage.Window.Eval("window");
            //Bottom = (double)System.Windows.Browser.HtmlPage.Window.Eval("window");
            Right = double.MaxValue;
            Bottom = double.MaxValue;
        }
    }

      //大家如果觉得那里有问题 ,请指正吧
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值