Reputation: 36866
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?
Upvotes: 3
Views: 4328
Reputation: 33379
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.
Upvotes: 12
Reputation: 292705
Maybe I misunderstood your problem, but isn't it exactly what a GridView does ?
Upvotes: 1