I’m tring to move a button in WP7 using MouseLeftButtonDown, MouseMove and MouseLeftButtonUp. The problem is when I move it (by mouse) it looks unstable and I can’t explain it.
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;
}
Am I doing something wrong? Thanks in advance!
You should use a
Canvascontrol as yourButtoncontrol’s container. Then you can specifyCanvas.LeftandCanvas.Topvalues on yourButtonand move it properly, without having to play with the size and margin. See here for an example related to your problem.