WP7:移动一个控件

时间:2012-02-06 12:36:00

标签: c# windows-phone-7 xaml drag

我想使用MouseLeftButtonDownMouseMoveMouseLeftButtonUp在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;
}

我做错了吗?提前谢谢!

1 个答案:

答案 0 :(得分:1)

您应该使用Canvas控件作为Button控件的容器。然后,您可以在Canvas.Left上指定Canvas.TopButton值并正确移动,而无需使用大小和边距。有关您的问题的示例,请参阅here