1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Windows; 7 using System.Windows.Controls; 8 using System.Windows.Data; 9 using System.Windows.Documents; 10 using System.Windows.Input; 11 using System.Windows.Media; 12 using System.Windows.Media.Imaging; 13 using System.Windows.Navigation; 14 using System.Windows.Shapes; 15 16 namespace WpfApplication1 17 { 18 /// <summary> 19 /// MainWindow.xaml 的交互逻辑 20 /// </summary> 21 public partial class MainWindow : Window 22 { 23 public MainWindow() 24 { 25 InitializeComponent(); 26 Binding binding = new Binding(); 27 binding.Source = main_grid; 28 binding.Path = new PropertyPath("Margin"); 29 binding.Mode = BindingMode.TwoWay; 30 binding.Converter = new MarginConverter(); 31 binding.ConverterParameter = main_grid.Width; 32 sub_grid.SetBinding(Grid.MarginProperty, binding); 33 } 34 35 public class MarginConverter : IValueConverter 36 { 37 38 public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture) 39 { 40 Thickness margin = (Thickness)value; 41 double width = (double)parameter; 42 return new Thickness(margin.Left + width, margin.Top -30, 0, 0); 43 } 44 45 public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture) 46 { 47 Thickness margin = (Thickness)value; 48 double width = (double)parameter; 49 return new Thickness(margin.Left - width, margin.Top + 30, 0, 0); 50 } 51 } 52 53 //选中控件的鼠标位置偏移量 54 Point targetPoint; 55 56 private void canvas_MouseDown(object sender, MouseButtonEventArgs e) 57 { 58 var targetElement = e.Source as IInputElement; 59 if (targetElement != null) 60 { 61 targetPoint = e.GetPosition(targetElement); 62 //开始捕获鼠标 63 targetElement.CaptureMouse(); 64 } 65 } 66 67 private void canvas_MouseUp(object sender, MouseButtonEventArgs e) 68 { 69 //取消捕获鼠标 70 Mouse.Capture(null); 71 } 72 73 private void canvas_MouseMove(object sender, MouseEventArgs e) 74 { 75 //确定鼠标左键处于按下状态并且有元素被选中 76 var targetElement = Mouse.Captured as Grid; 77 if (e.LeftButton == MouseButtonState.Pressed && targetElement != null) 78 { 79 var pCanvas = e.GetPosition(canvas); 80 //设置最终位置 81 targetElement.Margin = new Thickness(pCanvas.X - targetPoint.X, pCanvas.Y - targetPoint.Y, 0, 0); 82 } 83 } 84 } 85 }
1 <Window x:Class="WpfApplication1.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfApplication1" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="350" Width="525"> 9 <Canvas> 10 <Viewbox Stretch="Fill"> 11 <Grid x:Name="canvas" Height="350" Width="525"> 12 <Grid x:Name="main_grid" Height="50" Width="80" Background="Yellow" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="100,100,0,0" PreviewMouseMove="canvas_MouseMove" PreviewMouseLeftButtonDown="canvas_MouseDown" PreviewMouseLeftButtonUp="canvas_MouseUp"> 13 <Border BorderBrush="Black" BorderThickness="1" ></Border> 14 </Grid> 15 <Grid x:Name="sub_grid" Height="30" Width="30" Background="LightBlue" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="180,70,0,0" PreviewMouseMove="canvas_MouseMove" PreviewMouseLeftButtonDown="canvas_MouseDown" PreviewMouseLeftButtonUp="canvas_MouseUp"> 16 <Border BorderBrush="Black" BorderThickness="1"></Border> 17 </Grid> 18 </Grid> 19 </Viewbox> 20 </Canvas> 21 </Window>