Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a vertical scrollbar in my ListBox?

In the example below I have a ListBox with dozens of font names in it.

I would have thought it would automatically have a vertical scrollbar on it so that you can select ANY font, not just the first ones in the list, but it doesn't.

So I added a "ScrollViewer" and that puts a "scrollbar area" on the right but there is no scrollbar in the scrollbar area so that you can scroll (!).

Why isn't a scrollbar automatic and how do I force it to have a scrollbar?

<StackPanel Name="stack1">     <Grid>         <Grid.RowDefinitions>             <RowDefinition Height="2*"></RowDefinition>             <RowDefinition Height="*"></RowDefinition>         </Grid.RowDefinitions>         <ScrollViewer>             <ListBox Grid.Row="0" Name="lstFonts" Margin="3"  ItemsSource="{x:Static Fonts.SystemFontFamilies}"/>         </ScrollViewer>     </Grid> </StackPanel> 
like image 832
Edward Tanguay Avatar asked Jan 23 '09 13:01

Edward Tanguay


People also ask

How do I add a vertical scrollbar?

For vertical scrollable bar use the x and y axis. Set the overflow-x:hidden; and overflow-y:auto; that will automatically hide the horizontal scroll bar and present only vertical scrollbar. Here the scroll div will be vertically scrollable.

How do I add a vertical scroll bar in Visual Studio?

Double-click the form you want to use to display a scroll bar control. Drag and drop the control from the Visual Studio toolbox to the form. Use your mouse to place the scroll bar.

How can list box be made to scroll smoothly?

When the Firefox browser contained all listview items and you hold down the middle mouse button (but not release), and drag it, it should smoothly scroll the listview items.

What are vertical scrollbars?

A scroll bar's orientation determines the direction in which scrolling occurs when the user operates the scroll bar. A horizontal scroll bar enables the user to scroll the content of a window to the left or right. A vertical scroll bar enables the user to scroll the content up or down.


1 Answers

The problem with your solution is you're putting a scrollbar around a ListBox where you probably want to put it inside the ListBox.

If you want to force a scrollbar in your ListBox, use the ScrollBar.VerticalScrollBarVisibility attached property.

<ListBox      ItemsSource="{Binding}"      ScrollViewer.VerticalScrollBarVisibility="Visible"> </ListBox> 

Setting this value to Auto will popup the scrollbar on an as needed basis.

like image 177
JaredPar Avatar answered Oct 08 '22 07:10

JaredPar