Kelly
Kelly

Reputation: 3342

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>

Upvotes: 6

Views: 4079

Answers (1)

Jacob Adams
Jacob Adams

Reputation: 3994

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

Upvotes: 1

Related Questions