wpf拖拽封装类
<UserControl x:Class="WpfApplication1.DragItem" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" MouseDown="UserControl_MouseDown_1"> <Grid Background="#FF191919" HorizontalAlignment="Center" VerticalAlignment="Center"> <Label Content="项目文字" Foreground="White" FontSize="16" Width="450" Height="36" VerticalContentAlignment="Center" BorderThickness="2" BorderBrush="#FF424242"/> </Grid> </UserControl>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApplication1 { /// <summary> /// DragItem.xaml 的交互逻辑 /// </summary> public partial class DragItem : UserControl { public static Point dragDownMousePoint; public static Point dragDownControlPoint; public DragItem() { InitializeComponent(); } public DragItem(string content) { InitializeComponent(); this.Content.Content = content; } private void mouseMove(object sender, MouseEventArgs e) { Window w = Window.GetWindow(this); Point p = e.GetPosition(w); double moveX = p.X - dragDownMousePoint.X; double moveY = p.Y - dragDownMousePoint.Y; Point targetPoint = new Point(dragDownControlPoint.X + moveX, dragDownControlPoint.Y + moveY); if (double.IsNaN(dragDownControlPoint.X)) { targetPoint.X = moveX; } if (double.IsNaN(dragDownControlPoint.Y)) { targetPoint.Y = moveY; } Canvas parent = (Canvas)this.Parent; if (targetPoint.X < 0) { Canvas.SetLeft(this, 0); } else if (targetPoint.X + this.ActualWidth > parent.ActualWidth) { Canvas.SetLeft(this, parent.ActualWidth - this.ActualWidth); } else { Canvas.SetLeft(this, targetPoint.X); } if (targetPoint.Y< 0) { Canvas.SetTop(this, 0); } else if (targetPoint.Y + this.ActualHeight > parent.ActualHeight) { Canvas.SetTop(this, parent.ActualHeight - this.ActualHeight); } else { Canvas.SetTop(this, targetPoint.Y); } Console.WriteLine(targetPoint.X + this.ActualWidth + " " + parent.ActualWidth); } private void mouseUp(object sender, MouseEventArgs e) { Mouse.RemoveMouseMoveHandler(Window.GetWindow(this), mouseMove); Mouse.RemoveMouseUpHandler(Window.GetWindow(this), mouseUp); Mouse.Capture(null); } private void UserControl_MouseDown_1(object sender, MouseButtonEventArgs e) { Point p = e.GetPosition(Window.GetWindow(this)); dragDownMousePoint = p; dragDownControlPoint = new Point(Canvas.GetLeft(this), Canvas.GetTop(this)); Mouse.AddMouseMoveHandler(Window.GetWindow(this), mouseMove); Mouse.AddMouseUpHandler(Window.GetWindow(this), mouseUp); Mouse.Capture(this); } private void Label_Loaded_1(object sender, RoutedEventArgs e) { } } }