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);
Share:
112,447
Shamim Hafiz - MSFT
Author by

Shamim Hafiz - MSFT

Help and get helped

Updated on May 24, 2020

Comments

  • Shamim Hafiz - MSFT
    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
    Shamim Hafiz - MSFT almost 13 years
    Thanks. 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
    Rich almost 13 years
    I have no clue. Your question was two sentences long which is a bit little detail to anticipate any potential problems.
  • Shamim Hafiz - MSFT
    Shamim Hafiz - MSFT almost 13 years
    Got it, I was placing the opening tag in the wrong place. Thanks a lot for the help.
  • sedavidw
    sedavidw almost 13 years
    This code is taken out of context. Could you remove the dependencies, so the code is usable without further modifications?
  • O. R. Mapper
    O. R. Mapper about 11 years
    Well, 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?
  • vapcguy
    vapcguy over 7 years
    Repeat of previous answers.
  • Skinner
    Skinner almost 7 years
    from 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
    Jalil Markel about 4 years
    Simple and perfect answer.