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
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With