侧边栏壁纸
博主头像
Tony's Blog博主等级

行动起来,coding

  • 累计撰写 83 篇文章
  • 累计创建 58 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录
c#

WPF 拖拽帮助类

Tony
2024-02-24 / 0 评论 / 0 点赞 / 253 阅读 / 6528 字

本文链接: https://blog.csdn.net/lishuangquan1987/article/details/113628410

拖曳帮助代码:

public class DragDropHelper
    {
        #region Fields

        private FrameworkElement _sourceElement;
        private IEnumerable<FrameworkElement> _potentialTargets;
        protected bool m_IsDraging = false;
        protected Point m_DragStartPoint;
        #endregion

        #region ctors

        public DragDropHelper(FrameworkElement sourceElement)
        {
            if (sourceElement == null)
                throw new ArgumentNullException("sourceElement");

            this._sourceElement = sourceElement;
            this._sourceElement.PreviewMouseLeftButtonDown += (sender, e) =>
            {
                m_DragStartPoint = e.GetPosition(null);
            };
            this._sourceElement.PreviewMouseMove += HandleSourceElement_PreviewMouseMove;
        }
        #endregion

        #region Properties
        /// <summary>
        /// 拖拽操作的源
        /// </summary>
        public FrameworkElement SourceElement
        {
            get { return _sourceElement; }
        }
        /// <summary>
        /// 拖拽操作的目标元素集合
        /// </summary>
        public IEnumerable<FrameworkElement> PotentialTargets
        {
            get { return _potentialTargets; }
            set
            {
                if (_potentialTargets != null)
                {
                    foreach (var target in _potentialTargets)
                    {
                        target.PreviewDrop -= HandleTarget_PreviewDrop;
                        target.AllowDrop = false;
                    }
                }

                _potentialTargets = value;

                if (_potentialTargets != null)
                {
                    foreach (var target in _potentialTargets)
                    {
                        target.AllowDrop = true;
                        target.PreviewDrop += HandleTarget_PreviewDrop;
                    }
                }
            }
        }
        /// <summary>
        /// 是否可以进行拖拽的测试函数
        /// </summary>
        /// <remarks>
        /// args[0] - this DragDropHelper instance.
        /// </remarks>
        public Func<DragDropHelper, bool> CanDragSourcePredicter
        {
            get;
            set;
        }
        /// <summary>
        /// 响应放下操作的方法
        /// </summary>
        /// <remarks>
        /// args[0] - the receiver element.
        /// args[1] - the DragEventArgs object.
        /// </remarks>
        public Action<FrameworkElement, DragEventArgs> DropHandler
        {
            get;
            set;
        }
        /// <summary>
        /// 拖拽操作的数据对象创建函数
        /// </summary>
        /// <remarks>
        /// arg[0] - this DragDropHelper instance.
        /// </remarks>
        public Func<DragDropHelper, object> CreateDragData
        {
            get;
            set;
        }
        #endregion

        #region Methods

        private void HandleTarget_PreviewDrop(object sender, DragEventArgs e)
        {
            if (DropHandler != null)
            {
                DropHandler(sender as FrameworkElement, e);
            }
        }
        private void HandleSourceElement_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (CanDragSourcePredicter != null && !CanDragSourcePredicter(this))
                return;

            if (e.LeftButton == MouseButtonState.Pressed && !m_IsDraging)
            {
                Point position = e.GetPosition(null);

                if (e.LeftButton == MouseButtonState.Pressed && !m_IsDraging)
                {
                    position = e.GetPosition(null);

                    if (Math.Abs(position.X - m_DragStartPoint.X) > SystemParameters.MinimumHorizontalDragDistance ||
                    Math.Abs(position.Y - m_DragStartPoint.Y) > SystemParameters.MinimumVerticalDragDistance)
                    {
                        m_IsDraging = true;
                        DragDropEffects de = DragDrop.DoDragDrop(SourceElement, CreateDragData != null ? CreateDragData(this) : null, DragDropEffects.Copy);
                        m_IsDraging = false;
                    }
                }
            }
        }
        #endregion
    }

使用:

//支持拖放的控件目标集合
var list=new list<object>(){this.textbox1,this.textbox2,this.richTextBox1};

//treeView是拖放的控件源
new Helper.DragDropHelper(treeView)
{
   CanDragSourcePredicter = helper =>
   {
       return (helper.SourceElement as System.Windows.Controls.TreeView).SelectedItem as Person!= null;
   },
   CreateDragData = helper =>
   {
       return new DataObject(((helper.SourceElement as System.Windows.Controls.TreeView).SelectedItem as Person).Name);
   },
   PotentialTargets = list,
   DropHandler = (receiver, e) =>
   {
       try
       {

           var name= (int)e.Data.GetData(typeof(string));
			//处理拖放操作

       }
       catch (Exception error)
       {

       }

   },
};

外国写的拖曳旋转WPF示例:

https://www.codeproject.com/Articles/22952/WPF-Diagram-Designer-Part-1

0

评论区