#1,003 – Set CanContentScroll to False for Smooth Scrolling in a ListBox

By default, the ListBox control scrolls intelligently, one item at a time.  Scrolling behavior in a ListBox is provided by a ScrollViewer.  By default, the CanContentScroll property of the containing ScrollViewer is set to true, indicating that the items panel (e.g. a StackPanel) is responsible for the scrolling.  The StackPanel scrolls one item at a time as you drag the scrollbar thumb.

1003-001 1003-002

If you want to allow scrolling by pixels, rather than by items, you can set the ScrollViewer.CanContentScroll property on the ListBox to false.  This delegates scrolling responsibility back to the ScrollViewer.

        <ListBox ItemsSource="{Binding ActorList}" Width="300"
                 ScrollViewer.CanContentScroll="False">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Image Source="{Binding Image}" Height="100"/>
                        <StackPanel Margin="10,0">
                            <TextBlock Text="{Binding FullName}" FontWeight="Bold" />
                            <TextBlock Text="{Binding Dates}"/>
                            <TextBlock Text="{Binding KnownFor}" FontStyle="Italic"/>
                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

1003-003

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

3 Responses to #1,003 – Set CanContentScroll to False for Smooth Scrolling in a ListBox

  1. aeronaught says:

    Sean, I owe you my life. The sentence, “This delegates scrolling responsibility back to the ScrollViewer” in this tip was the golden fact that I didn’t even know I was looking for.
    My evil Canvas is no longer stealing BringIntoView() cookies from its parent, so thanks for that. I was ready to start tearing my hair out after a bit more low mewling.
    If you ever need anything at all – car, house, contract on an adversary, just let me know.
    -Jon

  2. hughes says:

    Great!!! Thank u:D

Leave a comment