Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a ListBoxItem stretch Vertically

I would like to make a ListBox function like a Grid. Each time a new item is added it should look like a a new GridRow was added (with a height of star). So if there are two items they will each take up half of the available space. At some point the Grid row will be smaller than the items MinHeight at which point the Grid will expand and a containing ScrollViewer can kick in.

You will see this behavior with a grid inside a ScrollViewer. However, I need to get this working with a ListBox so I can just set the ItemsSource, create a DataTemplate and move on.

The problem with the default ListBox ItemsPanel is that it will not let my first item expand to fill all the available space.

UPDATE: Here's the code to get it working:

<ListBox VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" Width="Auto" Height="Auto">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Columns="1"></UniformGrid>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>
like image 504
Kelly Avatar asked Sep 09 '09 19:09

Kelly


1 Answers

This SO post has some pretty good information that seems relevant to your post WPF - Why Listbox items do not fill uniformgrid

like image 105
Jacob Adams Avatar answered Oct 02 '22 15:10

Jacob Adams