#451 – Data Binding Elements in a Collection to a Grid, Part IV
December 15, 2011 3 Comments
Our first attempt to bind elements in a collection to a Grid didn’t work as expected. We set the Grid.Row and Grid.Column properties in the data template, but the items did not show up in the correct row and column.
The problem is that when we’re setting the Grid.Row and Grid.Column properties on the Label control in the data template, these Label controls are not direct children of the Grid, so the properties have no effect.
We can look at the visual tree of the ItemsControl to see this. The direct child elements of the Grid are actually instances of a ContentPresenter.
What we really want to do is set the value of the Grid.Row and Grid.Column properties on the ContentPresenter, rather than the Label. I’ll show how to do that in the next post.