#451 – Data Binding Elements in a Collection to a Grid, Part IV

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.

 

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

3 Responses to #451 – Data Binding Elements in a Collection to a Grid, Part IV

  1. Pingback: Dew Drop – December 15, 2011 (#1,220) | Alvin Ashcraft's Morning Dew

  2. Pingback: #452 – Data Binding Elements in a Collection to a Grid, Part V « 2,000 Things You Should Know About WPF

  3. Pingback: #455 – Using ItemContainerStyle to Bind Data Elements in a Collection to a Grid « 2,000 Things You Should Know About WPF

Leave a comment