Overview
The WPF ScrollViewer shows scrollbars and enables scrolling when its content is larger than the available viewport.
When a ScrollViewer is placed inside a StackPanel, however, the scrollbars never appear and the content keeps growing downward regardless of how many items it holds.
This article explains that the behavior comes from how the layout system measures elements, and it organizes the container-based fixes along with the criteria for choosing between them.
Prerequisites / Environment
- Framework: .NET 6 or later / WPF
- Language: C# / XAML
- Target controls:
ScrollViewer,StackPanel,Grid,DockPanel - Architecture: applicable to both MVVM and code-behind
Problem
Placing a ScrollViewer inside a vertical StackPanel and filling it with many items often results in no scrollbar; the content extends past the visible area instead.
<StackPanel>
<TextBlock Text="Header" />
<ScrollViewer VerticalScrollBarVisibility="Auto">
<StackPanel>
<!-- many items -->
</StackPanel>
</ScrollViewer>
</StackPanel>
Even though VerticalScrollBarVisibility="Auto" is set, the ScrollViewer expands to the full height of its content and no scrollbar is shown.
ScrollViewer sits inside a StackPanel: no scrollbar appears and the content is clipped at the bottom of the region. On the right it sits in a * row of a Grid, so a scrollbar appears and the remaining items are reachable.Cause / Background
The cause lies in the available size that the StackPanel passes to its children during measurement.
In its stacking direction, which is height for a vertical panel, a StackPanel measures each child with an infinite available size.
The official documentation notes that a StackPanel does not constrain its children in the direction it stacks.
A ScrollViewer shows a scrollbar only when its content is larger than the height it is given.
When the StackPanel passes an infinite height, the ScrollViewer requests exactly the height needed to fit all of its content, so no overflow occurs.
No scrollbar appears, and the ScrollViewer itself grows to the full height of its content.
The root of the problem is therefore not the ScrollViewer but the surrounding StackPanel, which never constrains the height.
Solution
Place the ScrollViewer in a container that constrains its height.
Specifically, put it in a Grid row sized with * (star), let a DockPanel fit it into the remaining space, or give it an explicit Height or MaxHeight.
A finite height is then passed to the ScrollViewer, and a scrollbar appears once the content exceeds that height.
Implementation
Place it in a star-sized Grid row
Split the Grid into a fixed-size row and a * row that fills the remaining space, and put the scrollable ScrollViewer in the * row.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="Header" />
<ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Auto">
<StackPanel>
<!-- many items -->
</StackPanel>
</ScrollViewer>
</Grid>
The * row receives the parent’s remaining height, so the ScrollViewer is given a finite height.
When the content exceeds that height, the scrollbar appears automatically.
Fit it into the remaining space with a DockPanel
A DockPanel expands its last child into the remaining area through LastChildFill, which is enabled by default.
Dock the header to the top and place the ScrollViewer as the last child.
<DockPanel>
<TextBlock DockPanel.Dock="Top" Text="Header" />
<ScrollViewer VerticalScrollBarVisibility="Auto">
<StackPanel>
<!-- many items -->
</StackPanel>
</ScrollViewer>
</DockPanel>
The last child without a DockPanel.Dock value fills the remaining area, so the height of the ScrollViewer is constrained.
Notes
VerticalScrollBarVisibility="Disabled"turns scrolling off: settingDisabledprevents scrolling in that direction through user interaction.
To hide the scrollbar while keeping scrolling, useHiddeninstead.- Avoid nesting scrollable controls directly: placing a control with its own scrolling, such as a
ListBox, directly inside aScrollViewercan make the mouse wheel act on the wrong element.
AListBoxalready scrolls internally, so it is better given a height-constrained layout, such as a*row of aGrid, than wrapped in an outerScrollViewer. - Physical versus logical scrolling:
ScrollViewer.CanContentScrolldefaults tofalse, giving physical, pixel-based scrolling; when it istrue, scrolling is logical, by item.
The standardListBoxtemplate sets it totrue, so a data-boundListBoxscrolls logically and itsVirtualizingStackPanelvirtualizes items.
That virtualization is lost only ifCanContentScrollis forced tofalse, so avoid that for long lists.
Alternatives / Comparison
| Container | Scrolling behavior | Suited for |
|---|---|---|
StackPanel (vertical) |
Does not constrain height, so the ScrollViewer does not scroll unless given an explicit Height or MaxHeight |
Short content that needs no scrolling |
Grid with a * row |
Passes the remaining height; scrolls when content overflows | Screens that separate headers or footers from a variable area |
DockPanel (LastChildFill) |
Fits into the remaining area; scrolls when content overflows | Screens combining edge-docked elements with a main area |
Explicit Height / MaxHeight |
Scrolls once the specified height is exceeded | A partial list with a fixed height cap |
Summary
A StackPanel passes an infinite height to its children in the stacking direction, so a ScrollViewer inside it cannot detect overflow and does not scroll.
The root of the problem is the surrounding container that fails to constrain the height, not the ScrollViewer itself.
The selection criteria are as follows.
- To separate headers or footers from a variable area: place the
ScrollViewerin a*row of aGrid.
Rows can be assigned by role, which makes the layout intent explicit. - To combine edge-docked elements with a main area: use a
DockPaneland place theScrollVieweras the last child in the remaining space. - To cap only the height of a partial list: set
MaxHeightand scroll once that height is exceeded.
In every case, the key is a layout that passes a finite height to the ScrollViewer.