WPF 拖拽帮助类

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
        /// 
        /// 拖拽操作的源
        /// 
        public FrameworkElement SourceElement
        {
     
            get {
      return _sourceElement; }
        }
        /// 
        /// 拖拽操作的目标元素集合
        /// 
        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;
                    }
                }
            }
        }
        /// 
        /// 是否可以进行拖拽的测试函数
        /// 
        /// 
        /// args[0] - this DragDropHelper instance.
        /// 
        public Func<DragDropHelper, bool> CanDragSourcePredicter
        {
     
            get;
            set;
        }
        /// 
        /// 响应放下操作的方法
        /// 
        /// 
        /// args[0] - the receiver element.
        /// args[1] - the DragEventArgs object.
        /// 
        public Action<FrameworkElement, DragEventArgs> DropHandler
        {
     
            get;
            set;
        }
        /// 
        /// 拖拽操作的数据对象创建函数
        /// 
        /// 
        /// arg[0] - this DragDropHelper instance.
        /// 
        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)
       {
     
         
       }

   },
};

你可能感兴趣的:(C#,c#,wpf)