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 ...
随机推荐
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第5章编程练习9
#include <iostream>using namespace std;int main(){ int num; cout<<"Enter number of ...
- jmeter下TPS插件的安装
1.下载插件http://pan.baidu.com/s/1mioVJni 2.解压下载的安装包: 将 jpgc-graphs-basic-2.0.zip 解压缩后只有一个 lib 目录,该目录下有一 ...
- C++调用C语言的库函数
在项目中,使用C语言编写了一个socket后台程序tkcofferd,并且为方便客户端的使用,提供了动态库,其中包含socket接口. 现在的需求是使用qt做一个前端界面,用来展示tkcofferd的 ...
- __x__(42)0910第六天__表格布局 老旧的布局方法
table 布局 不易于维护,耦合太严重了. 不利于搜索引擎检索. 效果图: html代码: <!doctype html> <html> <head> <m ...
- php的Memcached模块扩展
Memcached模块介绍 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态. ...
- C# WinForm:无法访问已释放的对象
C#在父窗口中调用子窗口的过程: 1. 创建子窗口对象 2. 显示子窗口对象 笔者的程序中,主窗体MainFrm通过菜单调用子窗口ChildFrm.在窗体中定义了子窗口对象,然后在菜单项点击事件中 ...
- sql server里中自增长的ID重新开始排
dbcc checkident('tablename',reseed,0); 执行:dbcc checkident('TableA',reseed,0); 执行结束:中途报了几次插入重复键. 结论:用 ...
- php 一行代码解决二维数组去重
array_unique($array, SORT_REGULAR);
- php composer windows安装
2018年6月22日10:40:49 笔记 1.先下载Composer-Setup.exe,下载地址:http://docs.phpcomposer.com/00-intro.html#Install ...
- 随笔二-https://www.cnblogs.com/shang1680/p/9657994.html
作业要求来自https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2097 GitHub远程仓库的地址:https://github.com/ ...