I have a canvas which contains a few textblocks and I need to find the top, left corner points that I assigned them in xaml. 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.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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
回答2:
Elegant solution
foreach (FrameworkElement fe in Canvas.Children)
Thickness margin = fe.Margin;
MessageBox.Show("Left: " + margin.Left + "Top: " + margin.Top);