#1,007 – Changing Scrollbar Settings for a ListBox

By default, the visibility of both horizontal and vertical scrollbars for a ListBox is set to Auto, indicating that the scrollbars should show up automatically, as needed.

You can change this behavior by setting the value of the ScrollViewer.HorizontalScrollBarVisibility or ScrollViewer.VerticalScrollBarVisibility properties.  They can take one of the following values:

  • Auto – Scrollbar visible if required
  • Visible – Scrollbar always visible
  • Hidden – Scrollbar not shown, but content allowed to scroll (e.g. using arrow keys)
  • Disabled – Content does not scroll
        <ListBox ItemsSource="{Binding ActorList}" Margin="10"
                 ScrollViewer.VerticalScrollBarVisibility="Auto"
                 ScrollViewer.HorizontalScrollBarVisibility="Disabled">
            <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>

1007-001

Advertisement

#1,006 – Scrollbars in a ListBox Appear as Needed

Scrolling in a ListBox is managed by a ScrollViewer, which contains the items panel of the ListBox.  The ScrollViewer provides scrolling logic and makes available both a horizontal and a vertical scrollbar.

The visibility of the scrollbars in a ScrollViewer is controlled by the HorizontalScrollBarVisibility and VerticalScrollBarVisibility properties.  By default, both of these properties are set to Auto for the ScrollViewer used in a ListBox.  This means that the scrollbar only appears if the content of the ListBox does not fit in the associated dimension.

1006-0011006-002

 

1006-003

#390 – Scrollbar Visibility in a ScrollViewer

The HorizontalScrollbarVisibility and VerticalScrollbarVisibility properties dictate which scrollbars appear in a ScrollViewer.  The properties can be set to one of the ScrollBarVisibility values:

  • Disabled – scrollbar doesn’t appear and user can’t scroll any of the contained content

  • Hidden – scrollbar doesn’t appear, but the user can still scroll the content, e.g. with arrow keys

  • Visible – scrollbar always visible, but greyed out if there is nothing to scroll

  • Auto – Scrollbar appears only when needed

#388 – Layout Containers Don’t Provide Scrolling Support

None of the WPF panel controls (containers) automatically provide scrollbars.  If their child controls do not fit in the available space, the child controls are clipped.

In the example below, we have a vertical StackPanel with a series of Label controls.  Notice that the first label doesn’t fit the width of the window and is clipped on the right.  The last label also does not fit and is clipped at the bottom of the window.