Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get ScrollViewer to work inside a StackPanel?

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> 
like image 850
Edward Tanguay Avatar asked Apr 29 '09 15:04

Edward Tanguay


People also ask

How to use ScrollViewer in Wpf?

There are two predefined elements that enable scrolling in WPF applications: ScrollBar and ScrollViewer. The ScrollViewer control encapsulates horizontal and vertical ScrollBar elements and a content container (such as a Panel element) in order to display other visible elements in a scrollable area.

What is StackPanel in XAML?

StackPanel is a layout panel that arranges child elements into a single line that can be oriented horizontally or vertically. By default, StackPanel stacks items vertically from top to bottom in the order they are declared. You can set the Orientation property to Horizontal to stack items from left to right.

What is StackPanel WPF?

A StackPanel allows you to stack elements in a specified direction. By using properties that are defined on StackPanel, content can flow both vertically, which is the default setting, or horizontally.


2 Answers

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:

  <ScrollViewer Grid.Row="1" Margin="299,12,34,54" Name="ScrollViewer1" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Height="195" CanContentScroll="True">         <StackPanel Name="StackPanel1" OverridesDefaultStyle="False"  Height="193" Width="376" VerticalAlignment="Top" HorizontalAlignment="Left"></StackPanel>   </ScrollViewer> 
like image 152
Rob Avatar answered Sep 26 '22 04:09

Rob


You can't without fixing the height of the StackPanel. It's designed to grow indefinitely in one direction. I'd advise using a different Panel. Why do you "need" to have an outer StackPanel?

like image 26
Kent Boogaart Avatar answered Sep 24 '22 04:09

Kent Boogaart