How to make WPF components take up all available space?

mvvm, wpf, wpf-controls, xaml

Solution

An ItemsControl puts the items in an ItemTemplate and puts those in the ItemsPanel.

The ItemsPanel can be adjusted by setting ItemsPanelTemplate.

By default the ItemsPanel is a StackPanel (vertical) and StackPanels do NOT distribute the rows/stretch the items vertically.

So what you could do is put a different control in the ItemsPanelTemplate that does stretch and align the items as you please.

Problem

I have an application that switches between usercontrols a lot, I want them to stretch to the max size of the window but something's preventing them from doing so, taking up the least possible amount of space instead. I noticed my components inside the usercontrols resize as they should (ie. buttons locked to the bottom of the control stay in place, the control is just not reaching the bottom of the window, where the buttons should eventually end up) and I haven't set any size-related properties on them (I tried several, but none had the desired effect). So I'm assuming the problem lies with the main component that displays the controls. That's set up like this: ``` <Window.Resources> <DataTemplate DataType="{x:Type vm:MessageViewModel}"> <vw:MessageView/> </DataTemplate> <DataTemplate DataType="{x:Type vm:ConnectionsViewModel}"> <vw:ConnectionView/> </DataTemplate> ... </Window.Resources> <Grid> <ItemsControl ItemsSource="{Binding ViewModels}" Margin="0,20,0,0"/> ... </Grid> ``` The solution I used thanks to Erno: Change ``` <ItemsControl ItemsSource="{Binding ViewModels}" Margin="0,20,0,0"/> ``` to ``` <ItemsControl ItemsSource="{Binding ViewModels}" Margin="0,20,0,0"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <Grid/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> </ItemsControl> ```

Original source