如何使ListBoxItem垂直伸展

Kel*_*lly 6 wpf listbox autosize

我想做一个ListBox像一个函数Grid.每次添加新项目时,它应该看起来像是添加了一个新项目GridRow(高度为星标).因此,如果有两个项目,它们将占用可用空间的一半.在某些时候,Grid行将小于将扩展的项目MinHeight,Grid并且包含ScrollViewer可以启动.

您将在一个网格内部看到此行为ScrollViewer.但是,我需要让这个工作,ListBox所以我可以设置ItemsSource,创建一个DataTemplate并继续前进.

默认问题ListBox ItemsPanel是它不会让我的第一个项目展开以填充所有可用空间.

更新:这是让它工作的代码:

<ListBox VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" Width="Auto" Height="Auto">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Columns="1"></UniformGrid>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>
Run Code Online (Sandbox Code Playgroud)

Jac*_*ams 1

这篇 SO 帖子有一些非常好的信息,似乎与您的帖子 WPF - Why Listbox items do not fill UniformGrid相关