如何使 ListBoxItem 垂直拉伸
我想制作一个像 Grid
一样的 ListBox
函数。每次添加新项目时,它应该看起来像添加了一个新的 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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个帖子有一些非常好的信息,似乎与您的帖子相关
WPF - 为什么列表框项目不填充uniformgrid
This SO post has some pretty good information that seems relevant to your post
WPF - Why Listbox items do not fill uniformgrid