Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WP7:移动控件_C#_Windows Phone 7_Xaml_Drag - Fatal编程技术网

C# WP7:移动控件

C# WP7:移动控件,c#,windows-phone-7,xaml,drag,C#,Windows Phone 7,Xaml,Drag,我尝试使用MouseLeftButtonDown、MouseMove和MouseLeftButtonUp移动WP7中的按钮。问题是当我(用鼠标)移动它时,它看起来不稳定,我无法解释 bool clicked = false; private void button1_MouseMove(object sender, MouseEventArgs e) { Point p = e.GetPosition(sender as Button); double margin1, marg

我尝试使用
MouseLeftButtonDown
MouseMove
MouseLeftButtonUp
移动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;
}

我做错什么了吗?提前谢谢

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

您应该改用操纵增量事件。该事件提供自上次触发事件以来鼠标移动的像素数。然后您可以将此值添加到控件的当前边距。谢谢!这很有帮助。我最后使用的是:
private void button1\u MouseMove(objectsender,MouseEventArgs e){Point p=e.GetPosition(MyCanvas);//如果(p.X>=0&&p.X=0&&p.Y@Downvoter-请解释downvote,因为如果答案被接受,这没有任何意义。如果你不同意答案,请发表评论解释不同意。