我发现这个CodeSnipped WWW。我很好用这个剪切的功能,但我唯一的问题是,当你移动一个UserControl你可以将控制移出窗口,它可以停用它吗?
public void MakeDraggable(System.Windows.UIElement moveThisElement, System.Windows.UIElement movedByElement)
{
bool isMousePressed = false;
System.Windows.Media.TranslateTransform transform = new System.Windows.Media.TranslateTransform(0, 0);
moveThisElement.RenderTransform = transform;
System.Windows.Point originalPoint = new System.Windows.Point(0, 0), currentPoint;
movedByElement.MouseLeftButtonDown += (a, b) =>
{
isMousePressed = true;
originalPoint = ((System.Windows.Input.MouseEventArgs)b).GetPosition(moveThisElement);
};
movedByElement.MouseLeftButtonUp += (a, b) => isMousePressed = false;
movedByElement.MouseLeave += (a, b) => isMousePressed = false;
movedByElement.MouseMove += (a, b) =>
{
if (!isMousePressed) return;
currentPoint = ((System.Windows.Input.MouseEventArgs)b).GetPosition(moveThisElement);
transform.X += currentPoint.X - originalPoint.X;
transform.Y += currentPoint.Y - originalPoint.Y;
};
}
感谢您的帮助,
弗里多林