I want to have a header, then below that a ScrollViewer
with an ItemsControl
, then below that a footer. Something like:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid ShowGridLines="True">
<Grid.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="36"/>
</Style>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0">Header</TextBlock>
<ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Auto">
<ItemsControl>
<ItemsControl.Items>
<TextBlock>Item 1</TextBlock>
<TextBlock>Item 2</TextBlock>
<TextBlock>Item 3</TextBlock>
<TextBlock>Item 4</TextBlock>
<TextBlock>Item 5</TextBlock>
</ItemsControl.Items>
</ItemsControl>
</ScrollViewer>
<TextBlock Grid.Row="2">Footer</TextBlock>
</Grid>
</Window>
The above is almost what I want, except the middle row is greedy; even if the window is very tall, it takes up as much space as it can, pushing the footer to the bottom of the window.
If I change the middle row's definition to Height="Auto"
, it takes up exactly the amount of space that it needs, even if that space isn't available, so the ScrollViewer
never shows the scrollbar, and the footer will get lost off the bottom of the window if the window isn't tall enough.
How do I make it so that if the window is tall enough for everything to fit, the footer is immediately below the ItemsControl
, but if the window isn't tall enough, the ScrollViewer
shows a scrollbar and the footer is at the bottom of the window?
I don't necessarily need to do this with a Grid
, but I didn't find any other Panel
that would do what I want either. E.g., a DockPanel
with the header set to DockPanel.Dock="Top"
, the footer set to DockPanel.Dock="Bottom"
, and the ItemsControl
filling the rest behaves exactly the same way.
Some other stuff I've tried:
- Setting
VerticalAlignment="Stretch"
on the footerTextBlock
: no change. - Making the footer row
Height="*"
: still not what I want; the footer and theItemsControl
get the same height, so the footer takes up too much space most of the time, or if you make the window extremely short, it goes off the bottom of the window.