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,因为如果答案被接受,这没有任何意义。如果你不同意答案,请发表评论解释不同意。