In MVVM, the way that worked for me was to bind the height of the ScrollViewer
to the ActualHeight
of the parent control (which is always of type UIElement
).
ActualHeight
is a read-only property which is only set after the control has been drawn onto the screen. It may change if the window is resized.
<StackPanel>
<ScrollViewer Height="{Binding Path=ActualHeight,
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UIElement}}">
<TextBlock Text=Hello"/>
</ScrollViewer>
</StackPanel>
But what if the parent control has an infinite height?
If the parent control has an infinite height, then we have a bigger problem. We have to keep setting the height of all parents, until we hit a control with a non-infinite height.
Snoop is absolutely invaluable for this:
If the "Height" for any XAML element is 0
or NaN
, you can set it to something using one of:
Height="{Binding Path=ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UIElement}}"
VerticalAlignment="Stretch"
Height="Auto"
Hint: Use VerticalAlignment="Stretch"
if you are a child of a Grid
with a <RowDefinition Height="*">
, and the Binding RelativeSource...
elsewhere if that doesn't work.
If you're interested, here is all of my previous attempts to fix this issue:
Appendix A: Previous Attempt 1
Can also use this:
Height="{Binding Path=ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=StackPanel}}"
Appendix B: Previous Attempt 2
Useful info: see Auto Height in combination with MaxHeight.
If nothing seems to work, it's probably because the ActualHeight
of the parent is either 0 (so nothing is visible) or huge (so the scrollviewer never needs to appear). This is more of a problem if there are deeply nested grids, with a scrollviewer right at the bottom.
- Use Snoop to find the
ActualHeight
of the parent StackPanel
. In properties, filter by the word "Actual"
, which brings back ActualHeight
and ActualWidth
.
- If
ActualHeight
is zero, give it a minimum height using MinHeight
, so we can at least see something.
- If
ActualHeight
is so huge that it goes off the edge of the screen (i.e. 16,000), give it a reasonable maximum height using MaxHeight
, so the scrollbars will appear.
Once the scrollbars are appearing, then we can clean it up further:
- Bind the
Height
of the StackPanel
or Grid
to the ActualHeight
of the parent.
Finally, put a ScrollViewer
inside this StackPanel
.
Appendix C: Previous Attempt 3
It turns out that this can sometimes fail:
Height="{Binding Path=ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=StackPanel}}"
The reason? It the binding fails, the height will be zero and nothing will be seen. The binding can fail if we are binding to an element which is not accessible. The binding will fail if we are going up
the visual tree, then down
to a leaf node (e.g. up to the parent grid, then down to the ActualHeight
of a row attached to that grid). This is why binding to the ActualWidth
of a RowDefinition
simply won't work.
Appendix D: Previous Attempt 4
I ended up getting this working by making sure that Height=Auto
for all of the parent elements from us to the first <Grid>
element in the UserControl.