16

I have a Canvas which contains a few Textblocks and I need to find the top and left corner points that were assigned in the XAML Document. How can I get those two properties?

When I loop through the Framework Elements on the Canvas I can't seem to find those to properties listed.

Ethirix
  • 13
  • 7
user337816
  • 365
  • 2
  • 5
  • 13

2 Answers2

27

Here some examples how to get the values:

foreach(FrameworkElement fe in canvas.Children){

   // example 0
   double top=(double)fe.GetValue(Canvas.TopProperty);
   double left=(double)fe.GetValue(Canvas.LeftProperty);

   // example 1
   double top1=Canvas.GetTop(fe);
   double left1=Canvas.GetLeft(fe);

}

See http://msdn.microsoft.com/en-us/library/ms749011.aspx and http://msdn.microsoft.com/en-us/library/system.windows.controls.canvas.top.aspx for more information

HCL
  • 36,053
  • 27
  • 163
  • 213
  • 1
    This should actually be double top = Canvas.GetTop(fe); – user337816 Jul 22 '10 at 21:20
  • 1
    Uups, better test before post tired:) Thanks for the info and the acceept. Have changed the examples. Gave you +1 for the question. – HCL Jul 22 '10 at 21:34
-2

Elegant solution

foreach (FrameworkElement fe in Canvas.Children)
         Thickness margin = fe.Margin;

MessageBox.Show("Left: " + margin.Left + "Top: " + margin.Top);
Community
  • 1
  • 1