#1,003 – Set CanContentScroll to False for Smooth Scrolling in a ListBox
February 6, 2014 3 Comments
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.
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>
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
Will do! Glad to help. No adversaries at the moment, but will keep you posted. :O)
Great!!! Thank u:D