wpf 在画布上查找控制位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3313277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find control position on canvas
提问by user337816
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.
我有一个包含一些文本块的画布,我需要找到我在 xaml 中分配给它们的左上角点。我怎样才能获得这两个属性?当我循环浏览画布上的框架元素时,我似乎无法找到列出的属性。
回答by HCL
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.aspxand http://msdn.microsoft.com/en-us/library/system.windows.controls.canvas.top.aspxfor more information
有关 更多信息,请参阅http://msdn.microsoft.com/en-us/library/ms749011.aspx和 http://msdn.microsoft.com/en-us/library/system.windows.controls.canvas.top.aspx信息
回答by user337816
Elegant solution
优雅的解决方案
foreach (FrameworkElement fe in Canvas.Children)
Thickness margin = fe.Margin;
MessageBox.Show("Left: " + margin.Left + "Top: " + margin.Top);