How can I stretch ListBoxItem vertically

I would like to make the ListBox function as a Grid . Each time a new element is added, it should look like a new GridRow been added (with star height). Therefore, if there are two elements, they will occupy half the available space. At some point, the Grid row will be smaller than the MinHeight elements, at that moment the Grid will expand, and the containing ScrollViewer may hit.

You will see this behavior with the grid inside ScrollViewer . However, I need to get this to work with the 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 allow my first item to expand to fill all the free space.

UPDATE: Here is the code to make it work:

 <ListBox VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" Width="Auto" Height="Auto"> <ListBox.ItemsPanel> <ItemsPanelTemplate> <UniformGrid Columns="1"></UniformGrid> </ItemsPanelTemplate> </ListBox.ItemsPanel> </ListBox> 
+6
wpf listbox autosize
source share
1 answer

There is pretty good information in this SO entry that seems relevant to your WPF post - Why list items don't fill out a single option

+1
source share

All Articles