How do I position an element within a WPF Grid column?

Trap picture Trap · Apr 20, 2009 · Viewed 6.9k times · Source

Let's say I want to position an element at the coordinates x=20, y=5 within the 3rd column of a Grid control. How do I do this? Do I need to add a Canvas panel to the column and then add the controls to it?

Answer

rajesh pillai picture rajesh pillai · Apr 20, 2009

You can do it that way, but have a look at this.. Attached Property

<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>

You can use the Margin property to simulate absolute positioning.

Not sure whether this would help you, but in any case an interesting read.