假设自定义一个用户控件用以在父容器Grid里拖动/移动:

<UserControl
x:Class="App6.Pic"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App6"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" ManipulationMode="All"
ManipulationStarted="UserControl_ManipulationStarted" ManipulationDelta="UserControl_ManipulationDelta"
d:DesignHeight="300" RenderTransformOrigin="0.5,0.5"
d:DesignWidth="400">
<UserControl.RenderTransform>
<TranslateTransform x:Name="t" />
</UserControl.RenderTransform>
<Grid Background="AliceBlue">
<TextBlock Name="txt" Text="" />
</Grid>
</UserControl>

  

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation; // The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236 namespace App6
{
public sealed partial class Pic : UserControl
{
public Pic()
{
this.InitializeComponent(); } private void UserControl_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
{ } private void UserControl_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{ this.t.X += e.Delta.Translation.X;
this.t.Y += e.Delta.Translation.Y;
txt.Text = "" + (Parent as Grid).ActualWidth + ", " + t.X + this.Margin.Left + this.ActualWidth / 2;
//判断核设置移动范围限制在父容器内
if ((Parent as Grid).ActualWidth < this.Margin.Left+ t.X + this.Width)//to right
{
this.t.X = (Parent as Grid).ActualWidth - (this.Margin.Left + this.Width);
} if (0 >this.Margin.Left + t.X )//to left
{
this.t.X = - this.Margin.Left ;
} if (0 > this.Margin.Top + t.Y)//to top
{
this.t.Y= -this.Margin.Top;
} if ((Parent as Grid).ActualHeight < this.Margin.Top + t.Y + this.Height)//to bottom
{
this.t.Y = (Parent as Grid).ActualHeight - (this.Margin.Top + this.Height);
} } }
}

  

UWP ManipulationStarted 移动图片或控件不要滑出父容器的判断的更多相关文章

  1. UI-UIImageView的图片填充方式(contentMode)_图片作为控件背景图的拉伸方式(stretch)介绍

    常用图片填充方式 这里只介绍三个最常用的图片填充方式 UIViewContentModeScaleToFill模式会导致图片变形.例如: UIViewContentModeScaleAspectFit ...

  2. 我写的一个 Qt 显示图片的控件

    Qt 中没有专门显示图片的控件.通常我们会使用QLabel来显示图片.可是QLabel 显示图片的能力还是有点弱.比方不支持图像的缩放一类的功能.使用起来不是非常方便. 因此我就自己写了个简单的类. ...

  3. Android开发技巧——定制仿微信图片裁剪控件

    拍照--裁剪,或者是选择图片--裁剪,是我们设置头像或上传图片时经常需要的一组操作.上篇讲了Camera的使用,这篇讲一下我对图片裁剪的实现. 背景 下面的需求都来自产品. 裁剪图片要像微信那样,拖动 ...

  4. MFC入门(三)-- MFC图片/文字控件(循环显示文字和图片的小程序)

    惯例附上前几个博客的链接: MFC入门(一)简单配置:http://blog.csdn.net/zmdsjtu/article/details/52311107 MFC入门(二)读取输入字符:http ...

  5. 关于IOS某图片添加控件,图片从相册或拍照保存后,再次进入时点击放大图无法显示的问题

    某图片添加控件: https://github.com/XZTLLQ/LQPhotoPickerDemo 问题: 标题已说明 代码块: NSArray *alAssetUrl =(NSMutableA ...

  6. 图片剪裁控件——ClipImageView

    这段时间在做自己的项目时,须要使用到图片剪裁功能,当时大概的思考了一些需求.想到了比較简单的实现方法.因此就抽了点时间做了这个图片剪裁控件--ClipImageView 这里先贴上ClipImageV ...

  7. Android实现图片滚动控件,含页签功能,让你的应用像淘宝一样炫起来

    首先题外话,今天早上起床的时候,手滑一下把我的手机甩了出去,结果陪伴我两年半的摩托罗拉里程碑一代就这么安息了,于是我今天决定怒更一记,纪念我死去的爱机. 如果你是网购达人,你的手机上一定少不了淘宝客户 ...

  8. 用MVVM模式开发中遇到的零散问题总结(5)——将动态加载的可视元素保存为图片的控件,Binding刷新的时机

    原文:用MVVM模式开发中遇到的零散问题总结(5)--将动态加载的可视元素保存为图片的控件,Binding刷新的时机 在项目开发中经常会遇到这样一种情况,就是需要将用户填写的信息排版到一张表单中,供打 ...

  9. UWP入门(五)--控件模板

    原文:UWP入门(五)--控件模板 通过在 XAML 框架中创建控件模板,你可以自定义控件的可视结构和可视行为(eg:勾选框的三种状态). 控件有多个属性,如 Background.Foregroun ...

  10. 一个 Qt 显示图片的控件(继承QWidget,使用QPixmap记录图像,最后在paintEvent进行绘制,可缩放)

    Qt 中没有专门显示图片的控件,通常我们会使用QLabel来显示图片.但是QLabel 显示图片的能力还是有点弱.比如不支持图像的缩放一类的功能,使用起来不是很方便.因此我就自己写了个简单的类. 我这 ...

随机推荐

  1. 国内985大学计算机方向硕博高性价比灌水的SCI期刊pr、kbs、eswa、ieee tii、ieee tiv具体都是啥?

    国内985大学计算机方向硕博高性价比灌水的SCI期刊pr.kbs.eswa.ieee tii.ieee tiv,下面给出具体名称: Pattern Recognition(PR) Knowledge- ...

  2. 《用广义CNOT门产生质数幂维的图态》

    参考文献:Graph states of prime-power dimension from generalized CNOT quantum circuit 主机文件:<2016质数图态.p ...

  3. 拿去面试!一个基于 DDD 的高性能短链系统

    众所周知,商城.RPC.秒杀.论坛.外卖.点评等项目早早就烂大街了,翻开同学的简历一看 10 个里面有 9 个是这些,翻遍全网再很难找到一个既有含金量又能看得懂的项目,针对此,我研发了这样一个可以快速 ...

  4. js获取nginx服务器时间

    前端页面js获取nginx服务器时间在实际开发中,我们通常要使用的是服务器端的时间,而不是本机电脑的时间,在js文件中直接通过new Date()获取的时间是本机电脑的系统时间,获取服务器时间的方法如 ...

  5. CAD Plus 使用帮助

    English help 移动端使用帮助 文档更新日期: 2023-07-28; 这篇文章将介绍如何使用CAD Plus app; 如果您有疑问或需要帮助请发送邮件至 3167292926@qq.co ...

  6. NSTableView 中 NSTextField无法输入

    NSTableView 中的 NSTextField 即使设置为Editable状态也无法编辑,需要生成一个NSTableView的子类并重载validateProposedFirstResponde ...

  7. 简单几步,基于云主机快速为Web项目添加AI助手

    在华为开发者空间,借助华为云对话机器人服务 CBS您可以零代码创建一个大模型 RAG (Retrieval-Augmented Generation,即检索增强生成)应用,来实现 AI 助手的智能问答 ...

  8. 『玩转Streamlit』--布局与容器组件

    在Streamlit中,布局类组件扮演着至关重要的角色. 它们不仅决定了应用程序的视觉呈现和用户体验,也极大地增强了页面内容的组织性和可读性. 通过这些组件,开发者可以灵活地划分页面空间,创建出清晰. ...

  9. curl访问etcd报错unable to set private key file

    [appdeploy@1a32vla0168zzzz cfssl]$ curl -i https://xxx.xxx.xxx.5:2379/health --cacert /app/etcd/cfss ...

  10. jQuery.validator验证无效的可能原因

    最近用jQuery.validator做表单的前端验证,却发现验证规则都无效.最后发现以下原因会导致校验无效 1.jquery.min.js重复引用. 2.js中有bug存在. 3.<input ...