Reputation: 122600
I have a ListBox
with enough items that it makes a vertical scrollbar visible. How can I make the list box automatically grow in height so it can display all items at once, without scrolling?
Thanks.
Upvotes: 1
Views: 236
Reputation: 189535
The problem is that at the core of the ListBox
template is this Xaml:-
<Border CornerRadius="2" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<ScrollViewer x:Name="ScrollViewer" Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}" BorderBrush="Transparent" BorderThickness="0" TabNavigation="{TemplateBinding TabNavigation}">
<ItemsPresenter />
</ScrollViewer>
</Border>
The ScrollViewer makes what you want to do quite difficult. The solution is to (assuming you really need a ListBox specifically) is to re-template the ListBox. Adjust the above portion of the template to:-
<Border CornerRadius="2" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
<ItemsPresenter />
</Border>
You would probably want to use StackPanel
as the items panel as well.
However if you don't need the selection features of ListBox then a simple ItemsControl would do it.
Upvotes: 4
Reputation: 5488
You have to make sure it's container will give it enough space. If the container for the ListBox
is a Grid
with fixed width and height then it will give the ListBox
a fixed size. When the height of the items exceeds that size then the scroll bar will become visible. If you put the ListBox
in a StackPanel
the StackPanel
will give the ListBox
infinite size. You still have to make sure the size of the StackPanel
is unbounded though.
Upvotes: 0