答案 0 :(得分:1)
您必须在画布上添加剪裁区域
默认剪切值为null。 (没有剪辑)
canvas.Clip = new RectangleGeometry();
canvas.Clip.Rect = new Rect(0, 0, canvas.ActualWidth, canvas.ActualHeight);
...
如果您因为包含ManipulationModes.TranslateInertia
而丢失了控制权(使用ManipulationMode.All)并且当您快速滑动dragableItem时很难控制,请尝试将ManipulationMode设置为此
DragableItem.ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY;
...
用于在翻译前检查边界
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;
}