#1,016 – Displaying a Collection of Items in a WrapPanel
February 25, 2014 1 Comment
You can use data binding to bind a collection of items to a WrapPanel by using the WrapPanel as the ItemsPanel for a simple ItemsControl. (You could do the same thing with a ListBox).
In the example below, we bind to a collection of Actor objects and set the DataTemplate of our ItemsControl to just display the image of each actor. The ItemsControl will resize to fit the containing window (via the StackPanel) and the WrapPanel will manage changing the layout of the images as the window size changes.
<ItemsControl ItemsSource="{Binding ActorList}" Margin="20"> <ItemsControl.ItemTemplate> <DataTemplate> <Image Source="{Binding Image}" Height="100"/> </DataTemplate> </ItemsControl.ItemTemplate> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <WrapPanel/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> </ItemsControl>