Smooth scrolling inside stackpanel in wpf

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

This is the code for the stack panel, which is populated at run time by several WrapPanels. Scroll Viewer scrolls the panels - one at a time - which makes it really inconvenient because all panels have different sizes. I tried this one by setting the ScrollViewer.CanContentScroll = "False" property in the StackPanel, and deleting it in the ScrollViewer did not help - the scroll bar disappeared altogether. What is the solution for a smooth scrollbar?

+6
source share
1 answer

Wrap the StackPanel in a different panel

WPF ScrollViewer tries to scroll all the elements in the view at the same time, so you see the dynamic scroll behavior. Nested by a StackPanel in another panel, ScrollViewer will try to scroll the entire StackPanel in a view that is too large, so it will use smooth scrolling.

Example: Removing a DockPanel will give you scroll scrolling, but with it you will get smooth scroll 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> 
+14
source

Source: https://habr.com/ru/post/903681/


All Articles