Smooth scroll within stackpanel in wpf

前端 未结 1 1591
有刺的猬
有刺的猬 2021-02-09 20:00

                

        
相关标签:
1条回答
  • 2021-02-09 20:25

    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>
    
    0 讨论(0)
提交回复
热议问题