Find control position on canvas

前端 未结 2 1333
挽巷
挽巷 2021-01-04 02:55

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

相关标签:
2条回答
  • 2021-01-04 03:34

    Elegant solution

    foreach (FrameworkElement fe in Canvas.Children)
             Thickness margin = fe.Margin;
    
    MessageBox.Show("Left: " + margin.Left + "Top: " + margin.Top);
    
    0 讨论(0)
  • 2021-01-04 03:41

    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

    0 讨论(0)
提交回复
热议问题