博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Grid move
阅读量:4986 次
发布时间:2019-06-12

本文共 3347 字,大约阅读时间需要 11 分钟。

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Windows; 7 using System.Windows.Controls; 8 using System.Windows.Data; 9 using System.Windows.Documents;10 using System.Windows.Input;11 using System.Windows.Media;12 using System.Windows.Media.Imaging;13 using System.Windows.Navigation;14 using System.Windows.Shapes;15 16 namespace WpfApplication117 {18     /// 19     /// MainWindow.xaml 的交互逻辑20     /// 21     public partial class MainWindow : Window22     {23         public MainWindow()24         {25             InitializeComponent();26             Binding binding = new Binding();27             binding.Source = main_grid;28             binding.Path = new PropertyPath("Margin");29             binding.Mode = BindingMode.TwoWay;30             binding.Converter = new MarginConverter();31             binding.ConverterParameter = main_grid.Width;32             sub_grid.SetBinding(Grid.MarginProperty, binding);33         }34 35         public class MarginConverter : IValueConverter36         {37 38             public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)39             {40                 Thickness margin = (Thickness)value;41                 double width = (double)parameter;42                 return new Thickness(margin.Left + width, margin.Top -30, 0, 0);43             }44 45             public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)46             {47                 Thickness margin = (Thickness)value;48                 double width = (double)parameter;49                 return new Thickness(margin.Left - width, margin.Top + 30, 0, 0);50             }51         }52 53         //选中控件的鼠标位置偏移量54         Point targetPoint;55 56         private void canvas_MouseDown(object sender, MouseButtonEventArgs e)57         {58             var targetElement = e.Source as IInputElement;59             if (targetElement != null)60             {61                 targetPoint = e.GetPosition(targetElement);62                 //开始捕获鼠标63                 targetElement.CaptureMouse();64             }65         }66 67         private void canvas_MouseUp(object sender, MouseButtonEventArgs e)68         {69             //取消捕获鼠标70             Mouse.Capture(null);71         }72 73         private void canvas_MouseMove(object sender, MouseEventArgs e)74         {75             //确定鼠标左键处于按下状态并且有元素被选中76             var targetElement = Mouse.Captured as Grid;77             if (e.LeftButton == MouseButtonState.Pressed && targetElement != null)78             {79                 var pCanvas = e.GetPosition(canvas);80                 //设置最终位置81                 targetElement.Margin = new Thickness(pCanvas.X - targetPoint.X, pCanvas.Y - targetPoint.Y, 0, 0);82             }83         }84     }85 }
1 
9
10
11
12
13
14
15
16
17
18
19
20
21

 

转载于:https://www.cnblogs.com/keyiei/p/6417297.html

你可能感兴趣的文章
题目1006:ZOJ问题
查看>>
使用 jackson 解析 json 演示样例
查看>>
C++内存分配方式详解——堆、栈、自由存储区、全局/静态存储区和常量存储区...
查看>>
维修U盘,那件小事
查看>>
php实现简单链式操作mysql数据库类
查看>>
JavaScript 常用正则表达式
查看>>
Torque2D MIT 学习笔记(1) ---- 了解
查看>>
如何通过命令行使用Wisdom RESTClient?
查看>>
class样式实现个人签名,一定字数后省略号取代后面内容
查看>>
设计模式之组合模式
查看>>
hdu_1690 (第一次做最短路)
查看>>
POJ 1321-棋盘问题
查看>>
漫谈测试人员和开发人员关系
查看>>
IOC
查看>>
Leetcode 374. Guess Number Higher or Lower
查看>>
统计学习方法一:基础
查看>>
2018-2019-1 20165236 《信息安全系统设计基础》第一周学习总结
查看>>
Jmeter-添加自定义函数
查看>>
每个Java程序员需要了解的8个Java开发工具
查看>>
【转】【Android】事件输入系统-代码层次解读
查看>>