如何在WPF网格列中定位元素?

Tra*_*rap 2 wpf grid wpf-positioning

假设我想在Grid控件的第3列中的坐标x = 20,y = 5处定位元素.我该怎么做呢?我是否需要向该列添加Canvas面板,然后将控件添加到该列中?

raj*_*lai 5

你可以这样做,但看看这个.. 附属物

<Grid Background="Yellow">
  <Rectangle Fill="Red" Margin="20,10,0,0" Width="50" Height="30"
     HorizontalAlignment="Left" VerticalAlignment="Top" />
  <Rectangle Stroke="Green" Margin="30,15,0,0" Width="20" Height="30"
     HorizontalAlignment="Left" VerticalAlignment="Top" />
</Grid>
Run Code Online (Sandbox Code Playgroud)

您可以使用Margin属性来模拟绝对定位.

不确定这是否会对你有所帮助,但无论如何都是有趣的阅读.