zoukankan      html  css  js  c++  java
  • WPF Canvas子控件拖拽行为

    1

    public class DragMoveBehavior: Behavior<UIElement>
        {
            Canvas parent;
            bool isDown;
            Point prePosition = new Point();
    
            protected override void OnAttached()
            {
                base.OnAttached();
    
                base.AssociatedObject.MouseLeftButtonDown += MouseLeftButtonDown;
                base.AssociatedObject.MouseLeftButtonUp += MouseLeftButtonUp;
                base.AssociatedObject.MouseLeave += MouseLeave;
    
                parent = LogicalTreeHelper.GetParent(base.AssociatedObject) as Canvas;
                parent.MouseLeftButtonUp += MouseLeftButtonUp;
                parent.MouseMove += MouseMove;
            }
    
            private void MouseMove(object sender, MouseEventArgs e)
            {
                if (!isDown) return;
                Point currentPosition = GetPosition(e);
    
                double offsetx = currentPosition.X - prePosition.X;
                double offsety = currentPosition.Y - prePosition.Y;
                double left = Canvas.GetLeft(AssociatedObject);
                double top = Canvas.GetTop(AssociatedObject);
                Console.WriteLine($"{left}, {top}");
                double l = double.IsNaN(left) ? 0 : left + offsetx;
                double t = double.IsNaN(top) ? 0 : top + offsety;
                Canvas.SetLeft(base.AssociatedObject, l);
                Canvas.SetTop(base.AssociatedObject, t);
                prePosition = currentPosition;
            }
    
            private void MouseLeave(object sender, MouseEventArgs e)
            {
                isDown = false;
                AssociatedObject.ReleaseMouseCapture();
            }
    
            private void MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
            {
                AssociatedObject.ReleaseMouseCapture();
                isDown = false;
            }
    
            private void MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                if (isDown) return;
                isDown = true;
                prePosition = GetPosition(e);
                AssociatedObject.CaptureMouse();
            }
    
            Point GetPosition(MouseEventArgs e)
            {
                return e.GetPosition(parent);
            }
    
            protected override void OnDetaching()
            {
                base.OnDetaching();
    
                base.AssociatedObject.MouseLeftButtonDown -= MouseLeftButtonDown;
                base.AssociatedObject.MouseLeftButtonUp -= MouseLeftButtonUp;
                base.AssociatedObject.MouseLeave += MouseLeave;
    
                parent.MouseLeftButtonUp -= MouseLeftButtonUp;
                parent.MouseMove -= MouseMove;
            }
        }

    2、调用方式:

    引用System.Windows.Interactivity类库;

    在xaml中添加

    xmlns:action="http://schemas.microsoft.com/expression/2010/interactivity"

    然后在想要移动的控件里添加如下代码,比如是想拖动button:

    <Button>
           <action:Interaction.Behaviors>
                  <local:DragMoveBehavior />
           </action:Interaction.Behaviors>
    </Button>
  • 相关阅读:
    利用Lambda获取属性名称
    Entity Framework 6.0 源码解读笔记(一)
    [转]Sql server2005中如何格式化时间日期
    python之路_RabbitMQ相关介绍
    python之路_redis相关介绍
    python之路_django之contenttype介绍
    python之路_最简单的Git介绍
    python之路_rest-framework之分页、路由、视图、渲染
    python之路_rest-framework之版本、解析器、序列化
    python之路_rest-framework之认证、权限、频率
  • 原文地址:https://www.cnblogs.com/RedSky/p/12193331.html
Copyright © 2011-2022 走看看