简体   繁体   English

在Window Store应用程序中的画布XAML上拖动元素并控制其移动

[英]Drag elements on canvas xaml in window store app and control its movement

I want to drag multiple items on canvas for this I use the following link this 我想拖在画布上多个项目为了这个,我使用下面的链接

it is dragging but it went out of canvas I want that to restrict its position inside canvas only . 它在拖动,但是已经脱离画布了,我只想限制它在画布内的位置。 What offset aur values to +/- ?? 什么将aur值偏移到+/- ??

Thanks. 谢谢。

You have to add clipping area to the canvas 您必须在画布上添加裁剪区域

by default clipping value is null. 默认情况下,裁剪值为null。 (No clipping) (不剪裁)

canvas.Clip = new RectangleGeometry();
canvas.Clip.Rect = new Rect(0, 0, canvas.ActualWidth, canvas.ActualHeight);

... ...

if you lost your control because you include ManipulationModes.TranslateInertia ( use ManipulationMode.All ) and it hard to control when you swipe dragableItem fast , try set your ManipulationMode to this 如果因为包含ManipulationModes.TranslateInertia (使用ManipulationMode.All)而失去控制,并且难以快速滑动dragableItem时进行控制,请尝试将ManipulationMode设置为此

DragableItem.ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY;

... ...

for checking boundary before making translation 在翻译之前检查边界

void DragableItem_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
    var translate = (TranslateTransform)DragableItem.RenderTransform;

    var newPosX = Canvas.GetLeft(DragableItem) + translate.X + e.Delta.Translation.X;
    var newPosY = Canvas.GetTop(DragableItem) + translate.Y + e.Delta.Translation.Y;

    if( ! isBoundary(newPosX,parentCanvas.ActualWidth - DragableItem.ActualWidth,0) )
        translate.X += e.Delta.Translation.X;
    if( !isBoundary(newPosY,parentCanvas.ActualHeight - DragableItem.ActualHeight,0))
        translate.Y += e.Delta.Translation.Y;

}
bool isBoundary(double value,double max,double min)
{
    return value > max ? true : value < min ? true : false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM