wpf 使用附加属性实现控件拖动

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApp1"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Button Content="Button" HorizontalAlignment="Left" Margin="100,100,0,0" VerticalAlignment="Top" Width="75" Height="23" local:DragMoveBehavior.CanDragMove="True"/>
    </Grid>
</Window>

项目中添加一个名为DragMoveBehavior的类,代码如下

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WpfApp1
{
    public static class DragMoveBehavior
    {
        public static readonly DependencyProperty CanDragMoveProperty =
            DependencyProperty.RegisterAttached("CanDragMove", typeof(bool), typeof(DragMoveBehavior), new PropertyMetadata(false, OnCanDragMoveChanged));

        public static bool GetCanDragMove(DependencyObject obj)
        {
            return (bool)obj.GetValue(CanDragMoveProperty);
        }

        public static void SetCanDragMove(DependencyObject obj, bool value)
        {
            obj.SetValue(CanDragMoveProperty, value);
        }

        private static void OnCanDragMoveChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is Control control)
            {
                if ((bool)e.NewValue)
                {
                    control.MouseLeftButtonDown += Control_MouseLeftButtonDown;
                    control.MouseLeftButtonUp += Control_MouseLeftButtonUp;
                    control.MouseMove += Control_MouseMove;
                }
                else
                {
                    control.MouseLeftButtonDown -= Control_MouseLeftButtonDown;
                    control.MouseLeftButtonUp -= Control_MouseLeftButtonUp;
                    control.MouseMove -= Control_MouseMove;
                }
            }
        }

        private static void Control_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (sender is Control control && e.LeftButton == MouseButtonState.Pressed)
            {
                control.CaptureMouse();
                control.Cursor = Cursors.Hand;
            }
        }

        private static void Control_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (sender is Control control && e.LeftButton == MouseButtonState.Released)
            {
                control.ReleaseMouseCapture();
                control.Cursor = Cursors.Arrow;
            }
        }

        private static void Control_MouseMove(object sender, MouseEventArgs e)
        {
            if (sender is Control control && e.LeftButton == MouseButtonState.Pressed)
            {
                var position = e.GetPosition(control.Parent as UIElement);
                var left = position.X - (control.ActualWidth / 2);
                var top = position.Y - (control.ActualHeight / 2);

                Canvas.SetLeft(control, left);
                Canvas.SetTop(control, top);
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潘诺西亚的火山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值