2012-02-06 70 views
0

我正在使用MouseLeftButtonDownMouseMoveMouseLeftButtonUp移动WP7中的按钮。问题是,当我移动它(通过鼠标)它看起来不稳定,我无法解释它。WP7:移动控件

bool clicked = false; 
private void button1_MouseMove(object sender, MouseEventArgs e) 
{ 
    Point p = e.GetPosition(sender as Button); 
    double margin1, margin2; 
    margin1 = p.X - (button1.ActualWidth/2) + 12; 

    // 12 is the distance between left of the page and the content panel 
    margin2 = p.Y - (button1.ActualHeight/2) + 161; 

    // 161 is the distance between top of the page and the content panel 
    button1.Margin = new Thickness(margin1, margin2, 0, 0); 
} 

private void button1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
{ 
    clicked = true; 
} 

private void button1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
{ 
    clicked = false; 
} 

我做错了什么?提前致谢!

+0

您应该使用ManipulationDelta事件。该事件为您提供自上次事件触发以来鼠标移动了多少像素。然后,您可以将此值添加到控件的当前边距。 – 2012-02-06 13:13:17

回答

1

您应该使用一个Canvas控件作为您的控件容器的Button。然后,您可以在您的Button上指定Canvas.LeftCanvas.Top值并正确移动,而无需使用尺寸和边距。有关您的问题的示例,请参见here

+1

谢谢!这有助于很多。我最后使用的是:'private void button1_MouseMove(object sender,MouseEventArgs e) { Point p = e.GetPosition(MyCanvas); (p.X> = 0 && p.X <= MyCanvas.ActualWidth) //获取画布内部的点 (p.Y> = 0&&p.Y <= MyCanvas.ActualHeight) ButtonToMove.SetValue(Canvas.TopProperty,p.Y - (ButtonToMove.ActualHeight/2)); }' – 2012-02-06 14:13:09

+0

@Downvoter - 请解释downvote,因为答案被接受是没有意义的。如果您不同意答案,请解释意见分歧。 – 2013-01-08 13:48:22