In my WPF application, I have a Stackpanel containing several controls inside them. How can I add a Scrollbar to this stackpanel.
Asked
Active
Viewed 1.3e+01k times
5 Answers
204
Put it into a ScrollViewer
.

Joey
- 344,408
- 85
- 689
- 683
-
1Thanks. After putting a ScrollViewer around it, the entire content doesn't show up. I tried setting Height and Width to Auto, but no luck. Why should only a limited portion show? – Shamim Hafiz - MSFT Jun 06 '11 at 09:57
-
8I have no clue. Your question was two sentences long which is a bit little detail to anticipate any potential problems. – Joey Jun 06 '11 at 09:59
-
Got it, I was placing the opening tag in the wrong place. Thanks a lot for the help. – Shamim Hafiz - MSFT Jun 06 '11 at 10:03
-
1Simple and perfect answer. – JM217 Feb 13 '20 at 13:47
125
Stackpanel doesn't have built in scrolling mechanism but you can always wrap the StackPanel in a ScrollViewer
<ScrollViewer VerticalScrollBarVisibility="Auto">
<StackPanel ... />
</ScrollViewer>

biju
- 17,554
- 10
- 59
- 95
-
3Well, `StackPanel` does implement `IScrollInfo` and offers a number of scrolling-related methods. Are you sure it does not have any kind of "built-in" scrolling mechanism? – O. R. Mapper Apr 23 '13 at 18:13
-
5from https://msdn.microsoft.com/en-us/library/system.windows.controls.stackpanel.canverticallyscroll(v=vs.110).aspx... "This property is not intended for use in your code. It is exposed publicly to fulfill an interface contract (IScrollInfo). Setting this property has no effect. If you require physical scrolling instead of logical scrolling, wrap the StackPanel in a ScrollViewer and set its CanContentScroll property to false." – Skinner Jul 25 '17 at 01:55
13
It works like this:
<ScrollViewer VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Disabled" Width="340" HorizontalAlignment="Left" Margin="12,0,0,0">
<StackPanel Name="stackPanel1" Width="311">
</StackPanel>
</ScrollViewer>
TextBox tb = new TextBox();
tb.TextChanged += new TextChangedEventHandler(TextBox_TextChanged);
stackPanel1.Children.Add(tb);

Esteban Verbel
- 738
- 2
- 20
- 39

arnisz
- 175
- 3
- 8
2
For horizontally oriented StackPanel, explicitly putting both the scrollbar visibilities worked for me to get the horizontal scrollbar.
<ScrollViewer VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Auto" >
<StackPanel Orientation="Horizontal" />
</ScrollViewer>

VijayKP
- 301
- 1
- 7
-3
If you mean, you want to scroll through multiple items in your stackpanel, try putting a grid around it. By definition, a stackpanel has infinite length.
So try something like this:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel Width="311">
<TextBlock Text="{Binding A}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" FontStretch="Condensed" FontSize="28" />
<TextBlock Text="{Binding B}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</Grid>
You could even make this work with a ScrollViewer

Freakishly
- 1,533
- 5
- 32
- 61
-
5This code is taken out of context. Could you remove the dependencies, so the code is usable without further modifications? – Markus Jarderot Jun 06 '11 at 10:07