tags:

views:

444

answers:

2

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
Thanks...didn't even know that existed
Rich
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
ok... just to make sure I understand : you want the items laid out horizontally instead of vertically ?
Thomas Levesque
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
+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 ColumnDefinitions 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
Yeah...that looks like the route I'm going.
Rich