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);
            }
        }
    }
}

你可能感兴趣的:(wpf)