XAML:
C#:
bool IsMouseDown = false;
Point mousePoint;
object mouseCtrl = null;
private void label1_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
IsMouseDown = true;
mousePoint = e.GetPosition(this.canvas1);
mouseCtrl = sender;
}
}
private void Window_MouseMove(object sender, MouseEventArgs e)
{
if (IsMouseDown)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
Point theMousePoint = e.GetPosition(this.canvas1 );
Canvas.SetLeft((UIElement)mouseCtrl, theMousePoint.X - (mousePoint.X - Canvas.GetLeft(((UIElement)mouseCtrl))));
Canvas.SetTop((UIElement)mouseCtrl, theMousePoint.Y - (mousePoint.Y - Canvas.GetTop(((UIElement)mouseCtrl))));
mousePoint = theMousePoint;
}
}
}
private void Window_MouseUp(object sender, MouseButtonEventArgs e)
{
if (IsMouseDown)
{
IsMouseDown = false;
}
}