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>.
Ну и жиртресты на фотках…
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?
Уважаемый Саша.
Ваше замечание не остроумно. Оставляйте замечания по делу, пожалуйста.
I bookmarked your blog, thanks for sharing this very interesting post
Саша, очень смешно! Я чуть со стула не упал когда прочел.
Гриша, +1!
Pашенги атакуют
Вам что ваших сайтов мало?
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!!
Good post. Thanks a lot. Great for C# noobs.
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
Саша +100500
Thanks, that was really helpful!