In the following WPF XAML the ScrollViewer does not work (it displays a scroll bar but you cannot scroll and the contents go off the window to the bottom).
I can change the outer StackPanel to a Grid and it will work.
However, in my application from which I reproduced the following code, I need to have an outer StackPanel. What do I have to do to the StackPanel to make the ScrollViewer show a usable scrollbar? e.g. VerticalAlignment="Stretch" Height="Auto" don't work.
<StackPanel>
<ScrollViewer>
<StackPanel>
<TextBlock Text="This is a test"/>
<TextBlock Text="This is a test"/>
<TextBlock Text="This is a test"/>
<TextBlock Text="This is a test"/>
<TextBlock Text="This is a test"/>
<TextBlock Text="This is a test"/>
<TextBlock Text="This is a test"/>
<TextBlock Text="This is a test"/>
<TextBlock Text="This is a test"/>
<TextBlock Text="This is a test"/>
<TextBlock Text="This is a test"/>
<TextBlock Text="This is a test"/>
<TextBlock Text="This is a test"/>
<TextBlock Text="This is a test"/>
<TextBlock Text="This is a test"/>
<TextBlock Text="This is a test"/>
<TextBlock Text="This is a test"/>
<TextBlock Text="This is a test"/>
<TextBlock Text="This is a test"/>
<TextBlock Text="This is a test"/>
<TextBlock Text="This is a test"/>
<TextBlock Text="This is a test"/>
<TextBlock Text="This is a test"/>
<TextBlock Text="This is a test"/>
<TextBlock Text="This is a test"/>
<TextBlock Text="This is a test"/>
<TextBlock Text="This is a test"/>
</StackPanel>
</ScrollViewer>
</StackPanel>
Indeed, the way I solved that dileman was to remove the outer stack panel and instead set the scrollviewer in the position I wanted inside the main grid.
This was bugging me for a while too, the trick is to put your stackpanel within a scrollviewer.
Also, you need to ensure that you set the CanContentScroll property of the scroll viewer to True, here's an example:
You can't without fixing the height of the
StackPanel
. It's designed to grow indefinitely in one direction. I'd advise using a differentPanel
. Why do you "need" to have an outerStackPanel
?Notice that sometimes you might have a StackPanel without realizing it. In my case I had this code
which worked fine. The "Pages" referenced by the binding was really different, complex UserControls, and I wanted to have only scrollbars on some of them. So I removed the scrollviewer:
And then I put the ScrollViewer as the top element on those of the usercontrols where I wanted them. However, this did not work. The content just flowed off the page. At first i didn't think this question/answer could help me, but the I realized that the default ItemPanel of an ItemsControl is the StackPanel. So I solved my problem by specifying an ItemsPanel that was not the StackPanel:
Moving Grid.Row="1" from StackPanel to ScrollViewer completely solved it for me.
I had a long list of some 40 items to show in a StackPanel, but only the first 20 were showing.
This is how it works:
By binding the ScrollViewer's Height to Window's Inner Height.
The logic of re-sizing is we need to give any element fix height or design the view to use render height.
Output: