Pavel Masyuk
Pavel Masyuk

Reputation: 133

How do i make StackPanel scroll content in one direction?

I want a scroll bar appear in a stack panel when there are too many items to fit in a screen, just like ListBox does. I can't just put it into a scroll viewer, because in that case horizontal StackPanel stops to scale elements verticaly. ScrollViewer tells its content, that it has infinite place to fit in, and items always keep their original size and ScrollViewer just crops elements and shows vertical scrollbar if height is too small or leaves blank space if it is too big. I probably need some tricky override for MeasureOverride() method of a ScrollViewer to make it content fit into that panel without making vertical scrollbar appear and take as much width as it wants, but all my attempts failed so far.

Upvotes: 0

Views: 281

Answers (1)

Fede
Fede

Reputation: 44028

The value of the ScrollViewer.HorizontalScrollBarVisibility and ScrollViewer.VerticalScrollbarVisibility will determine the layout behavior of the elements inside a ScrollViewer.

This:

<ScrollViewer VerticalScrollBarVisibility="Disabled" 
              HorizontalScrollBarVisibility="Auto">
   <StackPanel Orientation="Horizontal">
      <!-- ... -->
   </StackPanel>
</ScrollViewer>

will cause a horizontal scrollbar to appear when needed, while stretching (not scaling, which is something completely different) the StackPanel contents vertically.

Upvotes: 1

Related Questions