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. JavaScript中作用域和作用域链的简单理解(变量提升)

    通过阅读<JS高级程序设计>这本书,对js中的作用域和作用域链知识有了初步的了解和认识,准备成笔记供大家参考,笔记中字数比较多,但个人认为叙述的挺详细的,所以希望读者耐心看.再者,本人了解 ...

  2. PostgreSQL自学笔记:6 PostgreSQL函数

    6 PostgreSQL函数 6.2 数学函数 abs(x) 绝对值 pi() 圆周率π select abs(-3),pi(); cookie: MySQL中的pi()默认值3.141593, Po ...

  3. Android系统API综述

    下述能够找到Android开发源代码: 1. http://grepcode.com/project/repository.grepcode.com/java/ext/com.google.andro ...

  4. ubantu安全卸载火狐浏览器

    首先查看浏览器相应的安装包,命令: dpkg --get-selections |grep firefox 2.删除相面出现的包的命令: sudo apt-get purge pck1-name  p ...

  5. Cnr

    Description 写一个求阶乘的函数,实现Cmn排列,就是指从给定n个数的元素中取出指定r个数的元素,进行排序,我们用A(n,r)表示,组合,则是指从给定n个数的元素中仅仅取出指定r个数的元素, ...

  6. return的作用

    (1)终止函数的执行. (2)返回到调用的地方. (3)返回值.

  7. mysql 连接超慢

    cd /etc/mysql/mysql.conf.dsudo vi mysqld.cnf加上最后一句skip-name-resolve, 如下:[mysqld]## * Basic Settings# ...

  8. ECMA Script 6_必须要知道的基础

    ES6 为了保持兼容性,var 命令和 function 命令声明的全局变量,依旧是顶层对象的属性: 另一方面规定,let 命令.const 命令.class 命令声明的全局变量,不属于 window ...

  9. (97)Wangdao.com_第三十天_触摸事件

    触摸事件 只有触摸屏才会引发这一类事件 触摸事件 和 鼠标事件 同时触发,即使这个时候并没有用到鼠标. 这是为了让那些只定义鼠标事件.没有定义触摸事件的代码,在触摸屏的情况下仍然能用. 如果想避免这种 ...

  10. django 创建model(数据库表)失败

    在models.py中创建数据库表   from django.db import models # Create your models here. class Book(models.Model) ...