Radu Stefan
Radu Stefan

Reputation: 23

WP7: Move a control

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!

Upvotes: 0

Views: 853

Answers (1)

Samuel Slade
Samuel Slade

Reputation: 8623

You should use a Canvas control as your Button control's container. Then you can specify Canvas.Left and Canvas.Top values on your Button and move it properly, without having to play with the size and margin. See here for an example related to your problem.

Upvotes: 1

Related Questions