DrapMoveXBehavior.cs 4.32 KB
Newer Older
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
using Microsoft.Xaml.Behaviors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;

namespace FLY.ControlLibrary
{
    /// <summary>
    /// BUTTON 移动行为,附加行为。
    /// </summary>
    public class DrapMoveXBehavior : Behavior<UIElement>
    {

        //
        // 摘要:
        //     发生时 System.Windows.Controls.Button 时发生。
        [Category("Behavior")]
        public event EventHandler Move;

        bool isDown = false;
        Point downPoint;
        /// <summary>
        /// Move 事件滞后5秒触发
        /// </summary>
        public bool IsMoveEventDelayTrigger { get; set; } = true;


        T GetParent<T>(DependencyObject dobj) where T : DependencyObject
        {
            //一直向上找,直到找到window
            do
            {
                if (dobj is T)
                {
                    break;
                }
                dobj = System.Windows.Media.VisualTreeHelper.GetParent(dobj);
            } while (dobj != null);
            return (T)dobj;
        }


        DispatcherTimer timer = new DispatcherTimer();
        /// <summary>
        /// 
        /// </summary>
        public DrapMoveXBehavior()
        {
            timer.Tick += Timer_Tick;
            timer.Interval = TimeSpan.FromSeconds(3);
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            timer.Stop();
            Move?.Invoke(AssociatedObject, null);
        }
        /// <summary>
        /// 
        /// </summary>
        protected override void OnAttached()
        {
            base.OnAttached();
            UIElement dobj = AssociatedObject;

            dobj.MouseRightButtonDown += Dobj_MouseRightButtonDown;
            dobj.MouseRightButtonUp += Dobj_MouseRightButtonUp;
            dobj.MouseMove += Dobj_MouseMove;
            dobj.MouseLeave += Dobj_MouseLeave;

            parent = GetParent<Grid>(dobj);
        }

        private void Dobj_MouseLeave(object sender, MouseEventArgs e)
        {
            isDown = false;
        }

        private void Dobj_MouseMove(object sender, MouseEventArgs e)
        {


            //string json = Newtonsoft.Json.JsonConvert.SerializeObject(e);
            if (!isDown)
                return;
            FrameworkElement fe = (FrameworkElement)AssociatedObject;
            var downPoint = e.GetPosition(parent);

            string json = Newtonsoft.Json.JsonConvert.SerializeObject(downPoint);


            //移动
            int x = (int)(downPoint.X - this.downPoint.X);
            if (Math.Abs(x) < 2)
                return;

            this.downPoint = downPoint;
            var m = fe.Margin;
            if (fe.HorizontalAlignment == HorizontalAlignment.Center)
            {
                m.Right -= x * 2;
            }
            else if(fe.HorizontalAlignment == HorizontalAlignment.Right) 
            { 
                m.Right -= x;
            }
            else if(fe.HorizontalAlignment == HorizontalAlignment.Left)
            {
                m.Left += x;
            }
        
            
            fe.Margin = m;
            if (IsMoveEventDelayTrigger)
            {
                timer.Start();
            }
            else
            {
                Move?.Invoke(AssociatedObject, null);
            }
        }
        Grid parent;
        private void Dobj_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            downPoint = e.GetPosition(parent);
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(downPoint);
            isDown = true;
        }

        private void Dobj_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            isDown = false;
        }

        /// <summary>
        /// 
        /// </summary>
        protected override void OnDetaching()
        {
            base.OnDetaching();
            timer.Stop();

            UIElement dobj = AssociatedObject;

            dobj.MouseRightButtonDown -= Dobj_MouseRightButtonDown;
            dobj.MouseRightButtonUp -= Dobj_MouseRightButtonUp;
            dobj.MouseMove -= Dobj_MouseMove;
            dobj.MouseLeave -= Dobj_MouseLeave;
        }
    }
}