Scrolling in WPF

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.

wpf037

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.

wpf038

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>

wpf039

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>.

Advertisements

12 thoughts on “Scrolling in WPF”

  1. 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?

  2. Уважаемый Саша.

    Ваше замечание не остроумно. Оставляйте замечания по делу, пожалуйста.

  3. 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!!

  4. 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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s