【发布时间】:2013-07-13 21:43:27
【问题描述】:
我正在开发一个 Windows 应用商店应用程序。
我有一个 ListView,里面有一堆项目。它嵌套在一些堆栈面板和网格中,但它们基本上都是自动调整大小的。从本质上讲,ListView 占据了屏幕的大部分右半部分,而不管它的大小。
如果我给 ListView 一个硬值高度,滚动条将自动出现,无需额外工作。伟大的。但我不想设置高度......我希望它是其容器中可用的所有区域。如果我尝试变得聪明并将其设置为 9999 或其他内容,那么它将不会滚动。
我已经做了很多研究,类似的问题像this 说持有 ListView 的东西不能像 StackPanel 那样给它无限的大小。我有什么选择?如何将 ListView 放在具有任意空间的东西中并显示滚动条?
我唯一的想法是,必须有某种方法,在保存 ListView 的容器中,告诉 ListView 它具有所有可用区域,以使其高度设置为该值。有点像如何必须将 ListViewItem 的 HorizontalContentAlignment 属性设置为“Stretch”,以便让 ListView 中的项目知道它们实际具有的可用宽度。
我的布局的基本部分是这样的:
<!-- Nested in some other stuff simple Grids and StackPanels, none of which has hard heights set (all auto or *) -->
<!-- Even if I made this a Grid with one Row, setting definition to * or Auto doesn't help the issue - no scroll bar appears -->
<StackPanel>
<!-- Other stuff that has Visibility="Collapsed"... I have code so that only one item at a time within this container will ever be visible, and it gets all available space. -->
<ListView ItemsSource="{Binding SomeBigList}" ItemTemplate="{StaticResource MyDataTemplate}" />
<!-- Other stuff that has Visibility="Collapsed" -->
</StackPanel>
如何在不设置硬高度的情况下让滚动条出现在 ListView 中?谢谢你的帮助。
【问题讨论】:
-
Stackpanel 始终调整为包含其子项所需的最小大小(即它不会填充父容器)。您可以只使用几个 * 大小的嵌套网格,因为孩子会占用所有空间而不会被赋予无限大小
-
您是否尝试过将
StackPanel切换为DockPanel,然后在ListView中设置Height="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DockPanel}}, Path=ActualHeight}"
标签: wpf xaml windows-store-apps