Grid move
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的更多相关文章
- wx.grid.Grid
# -*- coding: cp936 -*- import wx import wx.grid import wx.lib.gridmovers as gridmovers import pymss ...
- 游戏编程模式 Game Programming Patterns (Robert Nystrom 著)
第1篇 概述 第1章 架构,性能和游戏 (已看) 第2篇 再探设计模式 第2章 命令模式 (已看) 第3章 享元模式 (已看) 第4章 观察者模式 (已看) 第5章 原型模式 (已看) 第6章 单例模 ...
- murri
github: https://github.com/haltu/muuri 官网:https://haltu.github.io/muuri/ 安装 npm install murri —sav ...
- R Customizing graphics
Customizing graphics GraphicsLaTeXLattice (Treillis) plots In this chapter (it tends to be overly co ...
- ggplot2学习笔记之图形排列
转载:https://www.jianshu.com/p/d46cf6934a2f R语言基本绘图函数中可以利用par()以及layout()来进行图形排列,但是这两个函数对于ggplot图则不太适用 ...
- [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 ...
- Dev Grid拖拽移动行
效果图 源码下载 拖拽时带行截图效果实现代码 /// <summary> /// 拖拽帮助类 /// </summary> public static class DragHe ...
- MFC Grid control 2.27
原文链接地址:http://www.codeproject.com/Articles/8/MFC-Grid-control MFCGridCtrl是个强大的类,用于数据的表格显示. 1.类特征 Cel ...
- Paths on a Grid(简单组合数学)
Paths on a Grid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 23008 Accepted: 5683 Desc ...
随机推荐
- css常用布局
1.一列布局 html: <div class="header"></div> <div class="body">< ...
- nodejs+koa在header里面添加header信息
参考:https://koa.bootcss.com/ ctx.append('resultCode', '0000'); ctx.append('resultMessage', 'success') ...
- DAY01-Python入门学习-计算机硬件
一.Python是编程语言 语言: 一种事物与另外一种事物沟通的介质所以说编程语言是程序员与计算机沟通的介质 什么是编程: 就是程序员用计算机所能理解的表达方式(编程语言)把自己的思维逻辑写下来,编程 ...
- Hibernate 单向一对多映射
单向 n-1: 单向 n-1 关联只需从 n 的一段访问 1 的一端 此处 Order 类和 Customer 类,其中 Order 类需要引用 Customer 类 代码: public class ...
- CSS_圣杯布局和双飞翼布局
参考: 圣杯布局的来历是2006年发在a list part上的这篇文章: http://alistapart.com/article/holygrail 双飞翼布局介绍-始于淘宝UED: http: ...
- windows 2008下IIS7 安装ASP.NET 遇到500.19
windows 2008下IIS7 安装ASP.NET 遇到如下错误: HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效. ...
- SSM的,日常错误
org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.imooc ...
- oracle统计数据时,涉及两个表的数据
SELECT t1.*,a.num FROM (SELECT SUM(t.total_profit) total_profit, SUM(t.main_business_income) main_bu ...
- IPV6配置
云友“学渣王 ”在帖子里提到需要为阿里云ECS配置IP6地址,根据网上的一些信息,写此例. 如果要求是FQDN地址,请登录到您的域名控制面板,设置一条AAAA记录到新设置的IPv6隧道地址. 在阿 ...
- word/pdf互转的链接
参考链接 http://www.greenxf.com/soft/98467.html