How to add a ScrollBar to a Stackpanel
112,447
Solution 1
Put it into a ScrollViewer
.
Solution 2
Stackpanel doesn't have built in scrolling mechanism but you can always wrap the StackPanel in a ScrollViewer
<ScrollViewer VerticalScrollBarVisibility="Auto">
<StackPanel ... />
</ScrollViewer>
Solution 3
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);
Comments
-
Shamim Hafiz - MSFT almost 4 years
In my WPF application, I have a Stackpanel containing several controls inside them. How can I add a Scrollbar to this stackpanel.
-
Shamim Hafiz - MSFT almost 13 yearsThanks. 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?
-
Rich almost 13 yearsI have no clue. Your question was two sentences long which is a bit little detail to anticipate any potential problems.
-
Shamim Hafiz - MSFT almost 13 yearsGot it, I was placing the opening tag in the wrong place. Thanks a lot for the help.
-
sedavidw almost 13 yearsThis code is taken out of context. Could you remove the dependencies, so the code is usable without further modifications?
-
O. R. Mapper about 11 yearsWell,
StackPanel
does implementIScrollInfo
and offers a number of scrolling-related methods. Are you sure it does not have any kind of "built-in" scrolling mechanism? -
vapcguy over 7 yearsRepeat of previous answers.
-
Skinner almost 7 yearsfrom msdn.microsoft.com/en-us/library/…... "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."
-
Jalil Markel about 4 yearsSimple and perfect answer.