srinivas
srinivas

Reputation: 89

How to change the position of a canvas child on mouse move?

I have many UI elements in a canvas object "CanvasContain". On mouse move I want to offset all the UI elements in that canvas. I tried with the name of the canvas, it is working fine:

foreach(UIElement child in CanvasContain.Children)
{
    if (child != null)
    { 
        Canvas2.Offset -= position - LastMousePosition;
        Canvas3.Offset -= position - LastMousePosition;
    }
}

But when I try with child.offset it is not working. How can I change the offset dynamically?

Upvotes: 0

Views: 1357

Answers (1)

yacc
yacc

Reputation: 3361

You need to adjust the Canvas Left and Top properties for each child:

foreach(UIElement child in CanvasContain.Children)
{
    double x = Canvas.GetLeft(child);
    double y = Canvas.GetTop(child);
    Canvas.SetLeft(child, x - (position.X - LastMousePosition.X));
    Canvas.SetTop (child, y - (position.Y - LastMousePosition.Y));
}

Note I dropped the test for child != null which isn't necessary.

Upvotes: 3

Related Questions