Is there an equivalent mechanism to the ItemsControl.ItemTemplate that works with a Grid? I have a collection of items and I'd like to present them as rows in a Grid so that I can assign Grid.Column to the individual elements inside the template (as opposed to rows in a list control). Is this possible in WPF using standard controls?
+1
A:
Maybe I misunderstood your problem, but isn't it exactly what a GridView does ?
Thomas Levesque
2009-10-28 20:39:35
Thanks...didn't even know that existed
Rich
2009-10-28 20:48:49
Actually, this is not at all what I'm looking for. I don't want all the extra fancy "data grid" formatting. I don't want column headers, the sorting functionality, the default padding and background, etc. I want to repeat items in a collection laid out in a grid the way I would normally do it in an ItemsControl...nothing more than that. Know of anything?
Rich
2009-10-28 20:57:10
ok... just to make sure I understand : you want the items laid out horizontally instead of vertically ?
Thomas Levesque
2009-10-28 21:15:32
yes, like a grid, but without all the overhead and fluff of the GridView. I actually decided on the same solution that Drew suggested. Using a regular Grid, but using SharedSizeGroup to bind the column widths outside of the DataTemplate
Rich
2009-10-28 21:18:09
+3
A:
Ok, use an ItemsControl with the Grid.IsSharedSizeScope="true"
attached property applied. Next, for your your ItemTemplate, you use a <Grid>
just like you normally would except now when you add ColumnDefinition
s you set the SharedSizeGroup
attribute to a name that is unique for each column. So for example:
<ItemsControl Grid.IsSharedSizeScope="true">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="MyFirstColumn" />
<ColumnDefinition SharedSizeGroup="MySecondColumn" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding MyFirstProperty}"/ >
<TextBlock Grid.Column="1" Text="{Binding MySecondProperty}"/ >
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
For more on IsSharedSizeScope and SharedSizeGroup, check out this section of the SDK. It should be noted that RowDefinitions also have a SharedSizeGroup so that you could do horizontal layouts as well.
Drew Marsh
2009-10-28 21:15:02