如何使 ListBoxItem 垂直拉伸

发布于 2024-08-04 05:53:26 字数 851 浏览 12 评论 0原文

我想制作一个像 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

就此别过 2024-08-11 05:53:26

这个帖子有一些非常好的信息,似乎与您的帖子相关
WPF - 为什么列表框项目不填充uniformgrid

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文