tags:

views:

2536

answers:

2

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 teh 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 (!).

What 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>
+6  A: 

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.

JaredPar
A: 

I added a "Height" to my ListBox and it added the scrollbar nicely.

Edward Tanguay
Try to avoid using the height and width properties as they can make changes down the road difficult. You're better off going with JaredPar's solution.
Bryan Anderson
I do that but it just puts a "scrollbar area" to the right with no scrollbar itself, and my list goes down past the bottom of my window down to all eternity, how can I tell the listbox (stop at the bottom of the window)?
Edward Tanguay
Because it's in a StackPanel, it's merrily being given all the space it needs, so it doesn't think it *needs* a scrollbar.
Donnelle