I am trying to make a UserControl draggable and I'm stuck and I have no idea why.
Basing myself on the answer here: How to drag a UserControl inside a Canvas
This portion of code is where I'm stuck:
private void Control_MouseMove(object sender, MouseEventArgs e)
{
var userControl= sender as UserControl;
Point currentPosition = e.GetPosition(userControl);
var transform = userControl.RenderTransform as TranslateTransform;
if (transform == null)
{
transform = new TranslateTransform();
userControl.RenderTransform = transform;
}
transform.X = currentPosition.X - clickPosition.X;
transform.Y = currentPosition.Y - clickPosition.Y;
}
}
What happens is that while dragging, the control jumps back and forth from its new position to it's old position.
When I change the above code to:
Point currentPosition = e.GetPosition(null);
The dragging works without the jumping back to the original position, but it is obviously offset. The UserControl in question does not have a parent control (in the sense that this.Parent is null).
The solution might be obvious but I'm just not seeing it. Any ideas?