2012-02-06 75 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