7
<ScrollViewer VerticalScrollBarVisibility="Auto" CanContentScroll="True">
                <StackPanel Name="basePanel" Orientation="Vertical" Height="450" />
            </ScrollViewer>

This is the code for the stackpanel which is filled in runtime with multiple WrapPanels. Scroll Viewer scrolls through the panels - one at a time - which makes it really inconvenient because all panels are of different sizes. I tried this one by setting ScrollViewer.CanContentScroll="False" property in StackPanel while deleting it in ScrollViewer, didn't help - scroll bar disappeared at all. What's the solution for smooth scroll bar?

Community
  • 1
  • 1
Sergey
  • 11,548
  • 24
  • 76
  • 113
  • I'm not sure but maybe you Stackpanel or some other container uses virtualization. WPF sometimes likes to apply a default "IsVirtualizing=true" property to many of its containers. – Dror Dec 13 '11 at 14:27

1 Answers1

15

Wrap your StackPanel in another panel

WPF's ScrollViewer tries to scroll entire elements into view at a time, which is why you see the jumpy scroll behavior. By nesting the StackPanel in another Panel, the ScrollViewer will try and scroll the entire StackPanel into view, which is too big so it will use smooth scrolling.

Here's an example - Removing the DockPanel will give you a jumpy scroll, but with it you'll get smooth scrolling behavior

<ScrollViewer VerticalScrollBarVisibility="Auto" CanContentScroll="True" Height="250">
    <DockPanel>
        <StackPanel Name="basePanel" Orientation="Vertical" Width="200">
            <Rectangle Height="75" Fill="Red" Width="200" />
            <Rectangle Height="50" Fill="Orange" Width="200" />
            <Rectangle Height="75" Fill="Yellow" Width="200" />
            <Rectangle Height="75" Fill="Green" Width="200" />
            <Rectangle Height="75" Fill="Black" Width="200"  />
            <Rectangle Height="75" Fill="Purple" Width="200" />
        </StackPanel>
    </DockPanel>
</ScrollViewer>
Rachel
  • 130,264
  • 66
  • 304
  • 490
  • I wrapped StackPanel with DockPanel but scroll bar has disappeared at all. – Sergey Dec 13 '11 at 15:14
  • @Sergey You have the `Height` set on your `StackPanel`, so it is limiting how large the the StackPanel can be. Move the `Height` attribute to your `ScrollViewer` and it should work. – Rachel Dec 13 '11 at 15:18
  • SO so funny and perfect solution ;) – kazem May 20 '13 at 16:10