Haiping said:
I need recreate a Canvas. I want to it looks like the same as the existing
Canvas.
How can I find each child location in the existing Canvas?
Thanks for any suggestion.
A straight forward way to go is using:
double top = Canvas.GetTop(myChildUIElement);
double left = Canvas.GetLeft(myChildUIElement);
Another method worth keeping in mind is the GetOffset
of the VisualTreeHelper static class - cause you can
use it for any UIElement whether its container is a
canvas of whatsoever.
foreach (UIElement ue in c1.Children)
{
FrameworkElement fe = ue as FrameworkElement;
string name = fe!=null && !string.IsNullOrEmpty(fe.Name) ?
fe.Name : "<unnamed UIElement>";
double top = Canvas.GetTop(ue);
double left = Canvas.GetLeft(ue);
Debug.Print("Type: {0}, Name: {1}, Left: {2} Top: {3}",
ue.GetType().Name, name, top, left);
Vector v = VisualTreeHelper.GetOffset(ue);
Debug.Print("Type: {0}, Name: {1}, Left: {2} Top: {3}",
ue.GetType().Name, name, v.X, v.Y);
}
HTH,
Alex