Frame91
Frame91

Reputation: 3789

ListBox with Stackpanel-Items - How to enable scroll? WPF

I'd like to enable a ScrollView for my ListBox. My ListBox is showing StackPanels. Unfortunately I'm not able to set a specific height to my ListBox (this is the only solution I found where I can use my Scroller).

What do I have to do to get a ScrollBar for my ListBox? (And even if I minimize/maximize the window it should appear if necessary)...

Thanks

Upvotes: 0

Views: 904

Answers (1)

qqbenq
qqbenq

Reputation: 10460

Well, the ListBox in WPF already contains a scroll, which you can force to be visible like this:

<ListBox 
    ScrollViewer.VerticalScrollBarVisibility="Visible">
</ListBox>

Most probably your problem comes from the fact, that your listbox resides in a control, which does not delimit it's height, so your listbox has a height that is exactly equal to the needed space (which may be more than your screen estate). Try to put it inside a Grid for example.

So to illustrate it with an example:

<StackPanel>
    <ListBox>
        <ListBox.Items>
            <TextBlock>Test</TextBlock>
            ...
        </ListBox.Items>
    </ListBox>
</StackPanel>

will never show a scrollbar, as the stackpanel will have a height that will be always enough for the listbox to show all elements, even though it might not be visible on the screen. But if you switch the StackPanel to a Grid in the above example you will have scrollbars when they are needed, as the Grid will constrain the height of the child control (the ListBox).

Hope this helps.

Upvotes: 2

Related Questions