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>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Binding binding = new Binding();
binding.Source = main_grid;
binding.Path = new PropertyPath("Margin");
binding.Mode = BindingMode.TwoWay;
binding.Converter = new MarginConverter();
binding.ConverterParameter = main_grid.Width;
sub_grid.SetBinding(Grid.MarginProperty, binding);
} public class MarginConverter : IValueConverter
{ public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Thickness margin = (Thickness)value;
double width = (double)parameter;
return new Thickness(margin.Left + width, margin.Top -, , );
} public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Thickness margin = (Thickness)value;
double width = (double)parameter;
return new Thickness(margin.Left - width, margin.Top + , , );
}
} //选中控件的鼠标位置偏移量
Point targetPoint; private void canvas_MouseDown(object sender, MouseButtonEventArgs e)
{
var targetElement = e.Source as IInputElement;
if (targetElement != null)
{
targetPoint = e.GetPosition(targetElement);
//开始捕获鼠标
targetElement.CaptureMouse();
}
} private void canvas_MouseUp(object sender, MouseButtonEventArgs e)
{
//取消捕获鼠标
Mouse.Capture(null);
} private void canvas_MouseMove(object sender, MouseEventArgs e)
{
//确定鼠标左键处于按下状态并且有元素被选中
var targetElement = Mouse.Captured as Grid;
if (e.LeftButton == MouseButtonState.Pressed && targetElement != null)
{
var pCanvas = e.GetPosition(canvas);
//设置最终位置
targetElement.Margin = new Thickness(pCanvas.X - targetPoint.X, pCanvas.Y - targetPoint.Y, , );
}
}
}
}
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Canvas>
<Viewbox Stretch="Fill">
<Grid x:Name="canvas" Height="350" Width="525">
<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">
<Border BorderBrush="Black" BorderThickness="1" ></Border>
</Grid>
<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">
<Border BorderBrush="Black" BorderThickness="1"></Border>
</Grid>
</Grid>
</Viewbox>
</Canvas>
</Window>