WPF Canvas: How to find its children's location?

H

Haiping

Hello,

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

Alexander Mueller

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top