I realized in my last post on WPF ListBoxes I overlooked a basic concept, how to make the ListBox scroll when there’s more items than can fit. Let’s return to the code from Monday, but resize the Window so only the top two pictures show. Running the app doesn’t change anything, the top two still show with no way to scroll down.
It turns out this is quite easy to fix, in several ways. First, many controls such as the containers (WrapPanel, for example) or listing controls (ListBox, TreeView to name two) have built in ScrollBars. The reason they did not appear in our example is because the ListBox was inside a StackPanel, and the StackPanel extended below the window edge. As such the ListBox didn’t realize it wasn’t fully displayed.
The simple way to fix then, is to remove the StackPanel and have the ListBox be the only thing on the window. As soon as we do, Vertical Scrollbars automatically appear.
Next, if we wanted to, we could wrap the entire panel in <ScrollViewer> tags. ScrollViewer is a XAML tag that will take whatever is inside and move with scrollbars, as needed. What you need to understand though is you’re moving the entire StackPanel, not just the ListBox.
To demonstrate, let’s add a text block to stack panel, just above the list box.
<ScrollViewer>
<StackPanel>
<TextBlock Background=“LightBlue“>Pick A Row</TextBlock>
<ListBox Name=“lbxCool“>
<!– ListBoxItems omitted for brevity –>
</ListBox>
</StackPanel>
</ScrollViewer>
Now if you run the program, you’ll see the text block scrolls up with the list box.
Note the text block has indeed scrolled up off the screen.
The best way to solve this would be to move the contents to a Grid. The grid sizes correctly take up the space, and allows the list box to automatically display it’s scrollbars.
<Grid Name=“myGrid“ >
<Grid.RowDefinitions>
<RowDefinition Height=“20“></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Grid.Row=“0“ Background=“LightBlue“>Pick A Row</TextBlock>
<ListBox Name=“lbxCool“ Grid.Row=“1“>
<!– ListBoxItems omitted for brevity –>
</ListBox>
</Grid>
If you need horizontal scrollbars, you can add the ScrollViewer.HorizontalScrollBarVisibility=“Visible” tag to the ListBox declaration.
And there you go, a few ways you can add scrolling to your ListBox, or to an entire container using the <ScrollViewer>.


December 3, 2007 at 1:14 am
Ну и жиртресты на фотках…
December 7, 2007 at 8:31 am
I am coding in C# and I want to scroll up and down. However, I want to place a button on the top and bottom of the Window Panel to accomplish the scrolling. Is this possible?
December 12, 2007 at 9:36 am
Уважаемый Саша.
Ваше замечание не остроумно. Оставляйте замечания по делу, пожалуйста.
October 12, 2008 at 11:29 am
I bookmarked your blog, thanks for sharing this very interesting post
February 23, 2010 at 9:29 am
Саша, очень смешно! Я чуть со стула не упал когда прочел.
March 3, 2010 at 5:34 am
Гриша, +1!
April 9, 2010 at 8:37 am
Pашенги атакуют
Вам что ваших сайтов мало?
August 29, 2010 at 2:40 pm
Hi maybe u could help me!
So I wrote the following code and I wanna put the grid into a scrollviewer but I cant figure out how could I? I wanna make the viewport scrollable when I zoom in the viewport!
If u know please help me!!
September 21, 2010 at 7:44 am
Good post. Thanks a lot. Great for C# noobs.
October 14, 2011 at 4:38 am
Thanks dude it is a great help for me
but when i put cursor on list box then it is unable to scroll and its scroll when click the down arrow of vertical bar
November 10, 2011 at 5:14 am
Саша +100500
November 10, 2011 at 10:53 am
Thanks, that was really helpful!