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>

Grid move的更多相关文章

  1. wx.grid.Grid

    # -*- coding: cp936 -*- import wx import wx.grid import wx.lib.gridmovers as gridmovers import pymss ...

  2. 游戏编程模式 Game Programming Patterns (Robert Nystrom 著)

    第1篇 概述 第1章 架构,性能和游戏 (已看) 第2篇 再探设计模式 第2章 命令模式 (已看) 第3章 享元模式 (已看) 第4章 观察者模式 (已看) 第5章 原型模式 (已看) 第6章 单例模 ...

  3. murri

    github: https://github.com/haltu/muuri 官网:https://haltu.github.io/muuri/   安装 npm install murri —sav ...

  4. R Customizing graphics

    Customizing graphics GraphicsLaTeXLattice (Treillis) plots In this chapter (it tends to be overly co ...

  5. ggplot2学习笔记之图形排列

    转载:https://www.jianshu.com/p/d46cf6934a2f R语言基本绘图函数中可以利用par()以及layout()来进行图形排列,但是这两个函数对于ggplot图则不太适用 ...

  6. [8.2] Robot in a Grid

    Imagine a robot sitting on the upper left corner of grid with r rows and c columns. The robot can on ...

  7. Dev Grid拖拽移动行

    效果图 源码下载 拖拽时带行截图效果实现代码 /// <summary> /// 拖拽帮助类 /// </summary> public static class DragHe ...

  8. MFC Grid control 2.27

    原文链接地址:http://www.codeproject.com/Articles/8/MFC-Grid-control MFCGridCtrl是个强大的类,用于数据的表格显示. 1.类特征 Cel ...

  9. Paths on a Grid(简单组合数学)

    Paths on a Grid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 23008 Accepted: 5683 Desc ...

随机推荐

  1. 【整理】Java 8新特性总结

    闲语: 相比于今年三月份才发布的Java 10 ,发布已久的Java 8 已经算是老版本了(传闻Java 11将于9月25日发布....).然而很多报道表明:Java 9 和JJava10不是 LTS ...

  2. css 绘制三角形

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. Android第四次作业

    一.团队成员 成员1:刘宇莹 学号:1600802122 班级:计算机164 博客链接:刘宇莹 成员2:孟鑫菲 学号:1600802092 班级:计算机163 博客链接:孟鑫菲 二.团队项目apk 拍 ...

  4. Hash算法的讲解

    散列表,又叫哈希表,它是基于快速存取的角度设计的,也是一种典型的“空间换时间”的做法.顾名思义,该数据结构可以理解为一个线性表,但是其中的元素不是紧密排列的,而是可能存在空隙. 散列表(Hash ta ...

  5. python联系-迭代器

    from collections import Iterable from collections import Iterator import time class Classmate(object ...

  6. iview select filterable属性使用下拉小bug

    今天做项目时候在iview 原生自带的select中设置filterable,下拉时候可进行查询,但是发现选中载打开模态框每次都绑定上一次的值,解决方案就是在关闭弹框时候将this.$refs.sto ...

  7. Vue.js的简介

    vue.js简介 Vue.js读音 /vjuː/, 类似于 view   Vue.js是前端三大新框架:Angular.js.React.js.Vue.js之一,Vue.js目前的使用和关注程度在三大 ...

  8. iOS开发支付篇-内购(IAP)

    一,前言 经典文章参考: . http://yimouleng.com/2015/12/17/ios-AppStore/ 内购流程 . http://www.jianshu.com/p/b199a46 ...

  9. react-router@4.0 使用和源码解析

    如果你已经是一个正在开发中的react应用,想要引入更好的管理路由功能.那么,react-router是你最好的选择~react-router版本现今已经到4.0.0了,而上一个稳定版本还是2.8.1 ...

  10. 继承 & 多态 & 封装

    什么是继承 继承是一种创建新类的方式,在python中,新建的类可以继承一个或多个父类,父类又可称为基类或超类,新建的类称为派生类或子类 python中类的继承分为:单继承和多继承 class Par ...