C# 面板wpf中图元的位置
如何在堆栈面板中查找子元素的位置C# 面板wpf中图元的位置,c#,wpf,xaml,C#,Wpf,Xaml,如何在堆栈面板中查找子元素的位置 <StackPanel Orientation="Horizontal"> <ToggleButton Width="20" Height="20" HorizontalAlignment="Stretch" VerticalAlignment
<StackPanel Orientation="Horizontal">
<ToggleButton Width="20"
Height="20"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Visibility="Visible" />
<TextBlock Margin="5"
VerticalAlignment="Center"
FontSize="15"
Text="Selection Mode" />
</StackPanel>
如何找到切换按钮和文本块的X、Y位置?基本上,控件的位置由保存它的控件、边距属性、对齐方式等决定
您可以使用它来确定子控件的位置。基本上,控件的位置由保存它的控件、边距属性、对齐方式等决定 您可以使用它来确定子控件的位置。您可以始终使用它将相对于一个UIElement的坐标转换为相对于另一个UIElement的坐标:
var toggleButtonPosition = toggleButton.TranslatePoint(new Point(0, 0), stackPanel);
var textBlockPosition = textBlock.TranslatePoint(new Point(0, 0), stackPanel);
上述代码将相对于相应控件的点(0,0)转换为相对于包含StackPanel的坐标,从而给出StackPanel内每个控件的位置。您可以始终使用该代码将相对于一个UIElement的坐标转换为相对于另一个UIElement的坐标:
var toggleButtonPosition = toggleButton.TranslatePoint(new Point(0, 0), stackPanel);
var textBlockPosition = textBlock.TranslatePoint(new Point(0, 0), stackPanel);
上述代码将相对于相应控件的点(0,0)转换为相对于包含StackPanel的坐标,从而给出StackPanel内每个控件的位置。@AndreasJohansson:与堆栈面板的关系。@AndreasJohansson:与堆栈面板的关系。