所以我有一个Border
,TranslateTransform
这样我就可以在我的窗口周围移动它。在Border
我的内容里面有一些控件加上一个ScrollViewer
. 拖动工作正常,但是当我单击滚动条时,整个边框会跳跃,因此我的光标现在位于我单击的最后一个点上。非常烦人,我不明白为什么它会发生在我的滚动条上而不是我的其他控件上。
(我正在使用 Denis Morozov 的简单、出色的指南http://denismorozov.blogspot.ie/2008/01/drag-controls-in-wpf-using.html)
这是我在代码中所做的;
private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
{
this.current.X = Mouse.GetPosition((IInputElement)sender).X;
this.current.Y = Mouse.GetPosition((IInputElement)sender).Y;
// Ensure object receives all mouse events.
this.current.InputElement.CaptureMouse();
}
private void Canvas_MouseUp(object sender, MouseButtonEventArgs e)
{
if (this.current.InputElement != null)
this.current.InputElement.ReleaseMouseCapture();
}
private void Canvas_MouseMove(object sender, MouseEventArgs e)
{
// if mouse is down when its moving, then it's dragging current
if (e.LeftButton == MouseButtonState.Pressed)
this.current.IsDragging = true;
else
this.current.IsDragging = false;
if (this.current.IsDragging && current.InputElement != null)
{
// Retrieve the current position of the mouse.
double newX = Mouse.GetPosition((IInputElement)sender).X;
double newY = Mouse.GetPosition((IInputElement)sender).Y;
// Reset the location of the object (add to sender's renderTransform
// newPosition minus currentElement's position
Transform rt = ((UIElement)this.current.InputElement).RenderTransform;
double offsetX = rt.Value.OffsetX;
double offsetY = rt.Value.OffsetY;
rt.SetValue(TranslateTransform.XProperty, offsetX + newX - current.X);
rt.SetValue(TranslateTransform.YProperty, offsetY + newY - current.Y);
// Update position of the mouse
current.X = newX;
current.Y = newY;
}
}
public void MouseLeftBtnDown(object sender, MouseButtonEventArgs e)
{
this.current.InputElement = (IInputElement)sender;
}