tags:

views:

382

answers:

1

In my ViewModel I have a list of items that I would like a grid in my view to bind to (the items will be the grids children). The list is a list of view models for the items.

How do you bind a grid to the list (I can access .children in code but not xaml)? Also, how do you specify the data template (another xaml file) for the view models in the list so that they are rendered correctly within the grid.

Thanks

+2  A: 

Use an ItemsControl with the ItemsPanel set to a Grid :

<ItemsControl ItemsSource="{Binding TheList}">
  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
        <Grid/>
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
</ItemsControl>

In the ItemsControl's ItemContainerStyle, you might want to bind the Grid.Row and Grid.Column attached properties to some property of the items :

  <ItemsControl.ItemContainerStyle>
    <Style TargetType="{x:Type FrameworkElement}">
        <Setter Property="Grid.Row" Value="{Binding RowIndex}"/>
        <Setter Property="Grid.Column" Value="{Binding ColumnIndex}"/>
    </Style>
  </ItemsControl.ItemContainerStyle>
Thomas Levesque
Thanks. I also figured out the second part of my question if anyone else comes across this<s:SurfaceUserControl.Resources> <DataTemplate DataType="{x:Type vm:ProductDetailsViewModel}"> <v:ProductDetails /> </DataTemplate> </s:SurfaceUserControl.Resources>
Dan dot net