Reputation: 89
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
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